Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand derive macros in the MacroExpander #39442

Merged
merged 4 commits into from
Feb 5, 2017

Conversation

keeperofdakeys
Copy link
Contributor

@keeperofdakeys keeperofdakeys commented Feb 1, 2017

This removes the expand_derives function, and sprinkles the functionality throughout the Invocation Collector, Expander and Resolver.

Fixes #39326

r? @jseyfried

@keeperofdakeys
Copy link
Contributor Author

r? @jseyfried

},
Err(Determinacy::Undetermined) if force => {
let path: Vec<_> = segments.iter().map(|seg| seg.identifier).collect();
let msg = format!("derive macro does not exist: '{}'", path[0].name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you just the original path here directly?
Eventually we'll allow e.g. #![derive(foo::bar)].
path.segments[0].identifier.name would also work for now, but I see no reason to collect a path: Vec<_>.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, how about "cannot find derive macro {} in this scope"?
c.f. this error

add_derived_markers(cx, attrs);
get_derive_attr(cx, attrs, DeriveType::Builtin)
}).or_else(|| {
get_derive_attr(cx, attrs, DeriveType::ProcMacro)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we switch the order here (i.e. ProcMacro first, then add_derived_markers/Builtin) for back-compat?

Since we want the input of proc macro derives to be fully expanded, we'll want to move in the direction of not including builtin derives, but I'd like to do that all at once with a Crater run.

@keeperofdakeys
Copy link
Contributor Author

So this PR has removed derive from the macro resolver, which has an interesting consequence. The first piece of code works on stable today, the second piece doesn't (but should with this change as it is currently). Obviously derive as an attribute macro is not going to work. Should anything be changed here?

// Works today
macro_rules! derive {
    () => { println!("HI!"); }
}

fn main() {
    derive!();
}
macro_rules! derive {
    () => { println!("HI!"); }
}

// Doesn't work today, except if this appears before the macro_rules!.
#[derive(Default)]
struct A;

fn main() {
    derive!();
}

@jseyfried
Copy link
Contributor

@keeperofdakeys
I don't think anything needs changing. I see this is a minor improvement, but since we're already fully special-casing #[derive(...)] I don't think it matters that much so long as we don't break things :)

@keeperofdakeys keeperofdakeys changed the title [WIP] Expand derive macros in the MacroExpander Expand derive macros in the MacroExpander Feb 2, 2017
@jseyfried
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Feb 2, 2017

📌 Commit b117bee has been approved by jseyfried

frewsxcv added a commit to frewsxcv/rust that referenced this pull request Feb 4, 2017
…eyfried

Expand derive macros in the MacroExpander

This removes the expand_derives function, and sprinkles the functionality throughout the Invocation Collector, Expander and Resolver.

r? @jseyfried
frewsxcv added a commit to frewsxcv/rust that referenced this pull request Feb 4, 2017
…eyfried

Expand derive macros in the MacroExpander

This removes the expand_derives function, and sprinkles the functionality throughout the Invocation Collector, Expander and Resolver.

r? @jseyfried
@frewsxcv
Copy link
Member

frewsxcv commented Feb 4, 2017

This PR might have resulted in this error during the rollup:

https://travis-ci.org/rust-lang/rust/jobs/198341670

#39537

---- [pretty] pretty/attr-variant-data.rs stdout ----

	

error: pretty-printed source does not typecheck

status: exit code: 101

command: /checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc - -Zno-trans --out-dir /checkout/obj/build/x86_64-unknown-linux-gnu/test/pretty/attr-variant-data.pretty-out --target=x86_64-unknown-linux-gnu -L /checkout/obj/build/x86_64-unknown-linux-gnu/test/pretty -L /checkout/obj/build/x86_64-unknown-linux-gnu/test/pretty/attr-variant-data.stage2-x86_64-unknown-linux-gnu.pretty.libaux -Crpath -O -Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers

stdout:

------------------------------------------

------------------------------------------

stderr:

------------------------------------------

error: cannot find derive macro `Serialize` in this scope

  --> <anon>:46:10

   |

46 | #[derive(Serialize, Deserialize)]

   |          ^^^^^^^^^

error: cannot find derive macro `Serialize` in this scope

  --> <anon>:30:10

   |

30 | #[derive(Serialize, Deserialize)]

   |          ^^^^^^^^^

error: cannot find derive macro `Serialize` in this scope

  --> <anon>:22:10

   |

22 | #[derive(Serialize, Deserialize)]

   |          ^^^^^^^^^

error: cannot find derive macro `Serialize` in this scope

  --> <anon>:19:10

   |

19 | #[derive(Serialize, Deserialize)]

   |          ^^^^^^^^^

error: aborting due to 4 previous errors

This allows builtin derives to be registered and
resolved, just like other derive types.
This removes the expand_derives function, and sprinkles
the functionality throughout the Invocation Collector,
Expander and Resolver.
@keeperofdakeys
Copy link
Contributor Author

keeperofdakeys commented Feb 5, 2017

@jseyfried So it looks like travis didn't run the pretty tests, one of which I'm failing (which I've confirmed locally). It involves parsing and pretty printing derive attributes? I'm not sure how the pretty printer is linked to the invocation collector though. Any thoughts?

@keeperofdakeys
Copy link
Contributor Author

keeperofdakeys commented Feb 5, 2017

Oh I think I understand now. With the custom_derive feature active, unknown derive macros were previously a warning. So the pretty print didn't bail out - however now it's an error.

The simple fix would be to remove the derives, or change them to builtin variants. Then maybe rename the serde attributes to something nonsensical, because it no longer makes semantic sense if the derive macros are no longer serde's.

Remove attr-variant-data.rs since it relies on quirks
in legacy custom derive resolution (undefined derives
only print a warning).

Add a new test which uses a defined proc macro derive,
and tests pretty printing of proc macro derive
attributes.
@keeperofdakeys
Copy link
Contributor Author

I deleted the failing test, since it didn't seem to add much after removing the custom derive parts (other tests test custom attributes). I also added a new test using a #[proc_macro_derive(Foo, attributes(Bar))], which should cover as much as the old test and proc macro derive attributes.

@jseyfried
Copy link
Contributor

@keeperofdakeys Great, thanks!
@bors r+

@bors
Copy link
Contributor

bors commented Feb 5, 2017

📌 Commit a201348 has been approved by jseyfried

frewsxcv added a commit to frewsxcv/rust that referenced this pull request Feb 5, 2017
…eyfried

Expand derive macros in the MacroExpander

This removes the expand_derives function, and sprinkles the functionality throughout the Invocation Collector, Expander and Resolver.

Fixes rust-lang#39326

r? @jseyfried
bors added a commit that referenced this pull request Feb 5, 2017
@bors bors merged commit a201348 into rust-lang:master Feb 5, 2017
bors added a commit that referenced this pull request Feb 6, 2017
[beta] Give a better error message for unknown derive messages

This PR improves the error message for unknown derive macros.

Currently unknown derives give the following error, which is very misleading:
```
`#[derive]` for custom traits is not stable enough for use. It is deprecated and will be removed in v1.15 (see issue #29644)
```

I'm currently working on a PR that will change this (#39442) to the following:
```
cannot find derive macro `Foo` in this scope
```

This PR backports the (as yet unmerged) error message to beta/1.16 (it's a pity that this is probably too late for 1.15).

r? @jseyfried
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants