-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Refactor macro resolution errors + add derive macro suggestions #39752
Conversation
All tests pass expect a few compile-fail tests. For some reason some macro invocations are no longer giving an error, I suspect this is a bug in my code (eg: src/test/compile-fail/macro-error.rs). Also type errors are also occuring in some tests, which leads to interesting bugs with dummy expansions (eg. src/test/compile-fail/macro-expansion-tests.rs). |
src/librustc_resolve/macros.rs
Outdated
@@ -260,21 +255,7 @@ impl<'a> base::Resolver for Resolver<'a> { | |||
|
|||
fn resolve_derive_macro(&mut self, scope: Mark, path: &ast::Path, force: bool) | |||
-> Result<Rc<SyntaxExtension>, Determinacy> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I'd refactor away this function now that it is a one-liner.
src/librustc_resolve/macros.rs
Outdated
find_best_match_for_name(self.macro_names.iter(), name, None), | ||
MacroInvocKind::Attr | | ||
MacroInvocKind::Derive => { | ||
// FIXME: get_macro needs an &mut Resolver, can we do it without cloning? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could probably mem::replace
the builtins -- I think the clone
on an error path is fine though.
src/libsyntax/ext/base.rs
Outdated
@@ -474,6 +474,35 @@ impl MacResult for DummyResult { | |||
pub type BuiltinDeriveFn = | |||
for<'cx> fn(&'cx mut ExtCtxt, Span, &MetaItem, &Annotatable, &mut FnMut(Annotatable)); | |||
|
|||
/// Represents different kinds of macro invocations that can be resolved. | |||
#[derive(Clone, Copy)] | |||
pub enum MacroInvocKind { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I'd name this MacroKind
to be more general -- for example, it also describes macro definitions. Also, the current name is pretty close to expand::InvocationKind
.
src/libsyntax/ext/base.rs
Outdated
|
||
impl MacroInvocKind { | ||
/// Returns true if the syntax extension is allowed for this macro invocation. | ||
pub fn allow_ext(&self, _ext: &Rc<SyntaxExtension>) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd instead add a method fn kind(&self) -> MacroKind { ... }
to SyntaxExtension
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't think that there was a 1-1 mapping, but I can see that there seems to be.
@@ -11,7 +11,7 @@ | |||
// ignore-tidy-linelength | |||
|
|||
#[derive(Eqr)] | |||
//~^ ERROR cannot find derive macro `Eqr` in this scope | |||
//~^ ERROR cannot find macro `Eqr` in this scope |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure that this is an improvement -- I think we should use "derive macro" (status quo) or "custom derive" here.
cc @nrc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually agree with this, and I was meaning to change it back. Should attribute macros show something different to "macro"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think attribute macros should say "cannot find attribute macro `...` in this scope"
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
src/librustc_resolve/macros.rs
Outdated
if suggestion != name { | ||
err.help(&format!("did you mean `{}!`?", suggestion)); | ||
if let MacroInvocKind::Bang = kind { | ||
err.help(&format!("did you mean `{}!`?", suggestion)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: indent
@@ -14,14 +14,14 @@ | |||
#![feature(asm)] | |||
#![feature(trace_macros, concat_idents)] | |||
|
|||
#[derive(Zero)] //~ ERROR | |||
#[derive(Zero)] | |||
struct CantDeriveThis; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd remove this -- confusing to have erroneous code that doesn't emit errors since we abort early.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was actually a bit confused about why we don't see a resolve error here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it's because we abort here due to other errors during expansion, so we never get to finalize_current_module_macro_resolutions
.
@@ -15,19 +15,24 @@ struct Self; | |||
|
|||
struct Bar<'Self>; | |||
//~^ ERROR lifetimes cannot use keyword names | |||
//~^^ ERROR parameter `'Self` is never used |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we move the struct Self;
in this test into a sub-module to avoid these extra errors?
@keeperofdakeys It looks like all test failures are due
I think the best solution would be to make sure we abort before ast->hir lowering if there were unresolved macro errors, at least until we figure out how to suppress the spurious type errors. |
That sounds good, but I don't know what to change to do that. Where is the code that starts the lowering? |
I would add a Btw, import resolution happens during expansion today, so this comment is outdated. |
Are attribute macros stored in |
@keeperofdakeys Normally, |
Also, we can import proc macro derives today via I think the best path forward here (in this PR or later) is to check |
☔ The latest upstream changes (presumably #39572) made this pull request unmergeable. Please resolve the merge conflicts. |
That sounds like a good idea, however I'm unsure how to go from a |
a1898f4
to
479c781
Compare
Mmm, for some reason the |
Oh I see now, |
This should do the trick.
Right, I don't think this is too hard to fix -- wherever we add This should actually be a simplification, since we could use e.g. |
a8f30a6
to
71679b5
Compare
It seems like it will be a bit of work to support |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! r=me modulo comments
src/librustc_driver/driver.rs
Outdated
// Since import resolution will eventually happen in expansion, | ||
// don't perform `after_expand` until after import resolution. | ||
// Since import resolution happs in expansion, don't perform | ||
// `after_expand` until after import resolution. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would just remove this comment -- since expansion and import resolution are interleaved, there's no way to perform after_expand
before import resolution, so we don't need to warn people about this.
Also, we can move after_expand(&krate)?
outside of time(time_passes, "name resolution", ...)
now, I think it would be best to put it just before expansion debugging diagnostics ("Post-expansion node count"
, etc.).
src/libsyntax/ext/base.rs
Outdated
@@ -474,6 +474,17 @@ impl MacResult for DummyResult { | |||
pub type BuiltinDeriveFn = | |||
for<'cx> fn(&'cx mut ExtCtxt, Span, &MetaItem, &Annotatable, &mut FnMut(Annotatable)); | |||
|
|||
/// Represents different kinds of macro invocations that can be resolved. | |||
#[derive(Clone, Copy, PartialEq)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I'd add Eq
just to be idiomatic.
@@ -20,14 +20,18 @@ pub fn main() { | |||
match 15 { | |||
ref Self => (), | |||
//~^ ERROR expected identifier, found keyword `Self` | |||
//~^^ ERROR match bindings cannot shadow unit structs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(optional) Could we avoid these extra errors by moving `struct (from earlier in the file) into a sub-module?
#[derive(Default)] //~ ERROR | ||
enum OrDeriveThis {} | ||
|
||
fn main() { | ||
doesnt_exist!(); //~ ERROR | ||
doesnt_exist!(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd remove this -- it's only not an error here since we abort early.
@bors delegate=keeperofdakeys |
✌️ @keeperofdakeys can now approve this pull request |
71679b5
to
2d91e7a
Compare
@bors r+ |
📌 Commit 2d91e7a has been approved by |
Refactor macro resolution errors + add derive macro suggestions Move legacy macro resolution error reporting to `finalize_current_module_macro_resolutions`, and provide suggestions for derive macros. Fixes #39323 cc #30197 r? @jseyfried
💔 Test failed - status-appveyor |
@bors: retry
* appveyor timeout
…On Thu, Feb 16, 2017 at 10:47 AM, bors ***@***.***> wrote:
💔 Test failed - status-appveyor
<https://ci.appveyor.com/project/rust-lang/rust/build/1.0.1980>
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#39752 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAD95CTYupbGjZxeqlDAVuPhuO7BqsXcks5rdH2JgaJpZM4L-WDv>
.
|
Refactor macro resolution errors + add derive macro suggestions Move legacy macro resolution error reporting to `finalize_current_module_macro_resolutions`, and provide suggestions for derive macros. Fixes #39323 cc #30197 r? @jseyfried
☀️ Test successful - status-appveyor, status-travis |
Move legacy macro resolution error reporting to
finalize_current_module_macro_resolutions
, and provide suggestions for derive macros.Fixes #39323
cc #30197
r? @jseyfried