-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Provide suggestions for unknown macros imported with use
#39953
Conversation
It looks like |
|
Agreed, this looks like the cleanest solution -- thanks @petrochenkov! @keeperofdakeys would you mind doing this instead of passing around the resolver more? |
Sure, sounds like a good idea. |
@jseyfried
|
Yes.
No, it can contain attribute proc macros today and bang proc macros once they are implemented.
Can't you get the |
I added some corner cases to the test cases, which I'm guessing were failing before all the changes in this PR. To fix it, I need to filter Also, are the changes to |
So for some reason derive proc-macros are appearing in |
src/librustc_metadata/decoder.rs
Outdated
} | ||
unreachable!(); |
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 think this would be clearer as an if !self.is_proc_macro(index) { ... } else { ... }
to avoid early returns.
Also, I would do let kind = self.proc_macros.as_ref().unwrap()[index.as_usize() - 1].1.kind();
in the else
case to avoid unreachable!()
.
src/librustc_resolve/macros.rs
Outdated
@@ -151,7 +151,7 @@ impl<'a> base::Resolver for Resolver<'a> { | |||
invocation.expansion.set(visitor.legacy_scope); | |||
} | |||
|
|||
fn add_ext(&mut self, ident: ast::Ident, ext: Rc<SyntaxExtension>) { | |||
fn add_ext(&mut self, ident: ast::Ident, kind: MacroKind, ext: Rc<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.
Since we have the SyntaxExtension
, I don't think we need to add the MacroKind
argument.
src/librustc_resolve/macros.rs
Outdated
MacroKind::Attr | | ||
MacroKind::Derive => { | ||
kind @ MacroKind::Attr | | ||
kind @ MacroKind::Derive => { |
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: no need to add kind @
here -- kind
is already in scope.
It looks like we add to |
@jseyfried Is the second place you told me to remove the insertion into |
Ah, I understand now. It was usually imported by the legacy_import function. For now I've added builtin_macros to the macro name check. |
Great, that's what I was going to suggest :) |
I think all the issues have been fixed now, so feel free to merge if you're happy with it. |
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.
Excellent! All nits are optional.
src/librustc_metadata/decoder.rs
Outdated
self.entry(index).kind.to_def(self.local_def_id(index)) | ||
} else { | ||
let kind = self.proc_macros.as_ref() | ||
.unwrap()[index.as_usize() - 1].1.kind(); |
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.
formatting nit: this line break here looks weird to me -- I'd put it all on one line or break after the ]
.
src/librustc_resolve/lib.rs
Outdated
if let Some(binding) = binding { | ||
contains = contains || binding.get_macro(self).kind() == MacroKind::Bang; | ||
} | ||
if primary_ns != MacroNS && path.len() == 1 && contains { |
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 would write this as
let is_builtin = self.builtin_macros.get(&path[0].name).cloned()
.map(|binding| binding.get_macro(self).kind() == MacroKind::Bang).unwrap_or(false);
if primary_ns != MacroNS && (is_builtin || self.macro_names.contains(&path[0].name)) { ... }
src/librustc_resolve/macros.rs
Outdated
find_best_match_for_name(self.macro_names.iter(), name, None) | ||
} else { | ||
None | ||
// Then check the legacy namespace. |
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 say "built-in macros' legacy namespace" or just "built-in macros" for clarity (I've referred to the scope of local macro_rules!
as "legacy" in item names and in comments).
src/librustc_resolve/macros.rs
Outdated
}; | ||
let ident = Ident::with_empty_ctxt(Symbol::intern(name)); | ||
self.lookup_typo_candidate(&vec![ident], MacroNS, is_macro) | ||
.map(|s| Symbol::intern(&s)) |
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: .as_ref().map(Symbol::intern)
might work here
src/librustc_resolve/macros.rs
Outdated
false | ||
} | ||
}; | ||
let ident = Ident::with_empty_ctxt(Symbol::intern(name)); |
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: Ident::from_str
should do the trick here.
@bors delegate=keeperofdakeys |
✌️ @keeperofdakeys can now approve this pull request |
This commit searchs modules for macro suggestions. It also removes imported macro_rules from macro_names, and adds more corner case checks for which macros should be suggested in specific contexts.
3c4e1ed
to
da6dc53
Compare
@bors r=jseyfried |
📌 Commit da6dc53 has been approved by |
…ried Provide suggestions for unknown macros imported with `use` cc rust-lang#30197 r? @jseyfried
…ried Provide suggestions for unknown macros imported with `use` cc rust-lang#30197 r? @jseyfried
Rollup of 28 pull requests - Successful merges: #39859, #39864, #39888, #39903, #39905, #39914, #39945, #39950, #39953, #39961, #39980, #39988, #39993, #39995, #40019, #40020, #40022, #40024, #40025, #40026, #40027, #40031, #40035, #40037, #40038, #40064, #40069, #40086 - Failed merges: #39927, #40008, #40047
cc #30197
r? @jseyfried