diff options
Diffstat (limited to '')
-rw-r--r-- | compiler/rustc_resolve/src/macros.rs | 56 |
1 files changed, 25 insertions, 31 deletions
diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 070fb9c72..9526296f9 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -12,7 +12,7 @@ use rustc_attr::StabilityLevel; use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::intern::Interned; use rustc_data_structures::sync::Lrc; -use rustc_errors::struct_span_err; +use rustc_errors::{struct_span_err, Applicability}; use rustc_expand::base::{Annotatable, DeriveResolutions, Indeterminate, ResolverExpand}; use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind}; use rustc_expand::compile_declarative_macro; @@ -112,47 +112,32 @@ fn fast_print_path(path: &ast::Path) -> Symbol { } } -/// The code common between processing `#![register_tool]` and `#![register_attr]`. -fn registered_idents( - sess: &Session, - attrs: &[ast::Attribute], - attr_name: Symbol, - descr: &str, -) -> FxHashSet<Ident> { - let mut registered = FxHashSet::default(); - for attr in sess.filter_by_name(attrs, attr_name) { +pub(crate) fn registered_tools(sess: &Session, attrs: &[ast::Attribute]) -> FxHashSet<Ident> { + let mut registered_tools = FxHashSet::default(); + for attr in sess.filter_by_name(attrs, sym::register_tool) { for nested_meta in attr.meta_item_list().unwrap_or_default() { match nested_meta.ident() { Some(ident) => { - if let Some(old_ident) = registered.replace(ident) { - let msg = format!("{} `{}` was already registered", descr, ident); + if let Some(old_ident) = registered_tools.replace(ident) { + let msg = format!("{} `{}` was already registered", "tool", ident); sess.struct_span_err(ident.span, &msg) .span_label(old_ident.span, "already registered here") .emit(); } } None => { - let msg = format!("`{}` only accepts identifiers", attr_name); + let msg = format!("`{}` only accepts identifiers", sym::register_tool); let span = nested_meta.span(); sess.struct_span_err(span, &msg).span_label(span, "not an identifier").emit(); } } } } - registered -} - -pub(crate) fn registered_attrs_and_tools( - sess: &Session, - attrs: &[ast::Attribute], -) -> (FxHashSet<Ident>, FxHashSet<Ident>) { - let registered_attrs = registered_idents(sess, attrs, sym::register_attr, "attribute"); - let mut registered_tools = registered_idents(sess, attrs, sym::register_tool, "tool"); // We implicitly add `rustfmt` and `clippy` to known tools, // but it's not an error to register them explicitly. let predefined_tools = [sym::clippy, sym::rustfmt]; registered_tools.extend(predefined_tools.iter().cloned().map(Ident::with_dummy_span)); - (registered_attrs, registered_tools) + registered_tools } // Some feature gates for inner attributes are reported as lints for backward compatibility. @@ -456,7 +441,7 @@ impl<'a> ResolverExpand for Resolver<'a> { } PathResult::Indeterminate => indeterminate = true, // We can only be sure that a path doesn't exist after having tested all the - // posibilities, only at that time we can return false. + // possibilities, only at that time we can return false. PathResult::Failed { .. } => {} PathResult::Module(_) => panic!("unexpected path resolution"), } @@ -605,9 +590,7 @@ impl<'a> Resolver<'a> { let res = if path.len() > 1 { let res = match self.maybe_resolve_path(&path, Some(MacroNS), parent_scope) { - PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => { - Ok(path_res.base_res()) - } + PathResult::NonModule(path_res) if let Some(res) = path_res.full_res() => Ok(res), PathResult::Indeterminate if !force => return Err(Determinacy::Undetermined), PathResult::NonModule(..) | PathResult::Indeterminate @@ -707,12 +690,23 @@ impl<'a> Resolver<'a> { Some(Finalize::new(ast::CRATE_NODE_ID, path_span)), None, ) { - PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => { - let res = path_res.base_res(); - check_consistency(self, &path, path_span, kind, initial_res, res); + PathResult::NonModule(path_res) if let Some(res) = path_res.full_res() => { + check_consistency(self, &path, path_span, kind, initial_res, res) } path_res @ PathResult::NonModule(..) | path_res @ PathResult::Failed { .. } => { + let mut suggestion = None; let (span, label) = if let PathResult::Failed { span, label, .. } = path_res { + // try to suggest if it's not a macro, maybe a function + if let PathResult::NonModule(partial_res) = self.maybe_resolve_path(&path, Some(ValueNS), &parent_scope) + && partial_res.unresolved_segments() == 0 { + let sm = self.session.source_map(); + let exclamation_span = sm.next_point(span); + suggestion = Some(( + vec![(exclamation_span, "".to_string())], + format!("{} is not a macro, but a {}, try to remove `!`", Segment::names_to_string(&path), partial_res.base_res().descr()), + Applicability::MaybeIncorrect + )); + } (span, label) } else { ( @@ -726,7 +720,7 @@ impl<'a> Resolver<'a> { }; self.report_error( span, - ResolutionError::FailedToResolve { label, suggestion: None }, + ResolutionError::FailedToResolve { label, suggestion }, ); } PathResult::Module(..) | PathResult::Indeterminate => unreachable!(), |