@@ -12,7 +12,7 @@ use rustc_attr::StabilityLevel;
1212use rustc_data_structures:: fx:: FxHashSet ;
1313use rustc_data_structures:: intern:: Interned ;
1414use rustc_data_structures:: sync:: Lrc ;
15- use rustc_errors:: struct_span_err;
15+ use rustc_errors:: { struct_span_err, Applicability } ;
1616use rustc_expand:: base:: { Annotatable , DeriveResolutions , Indeterminate , ResolverExpand } ;
1717use rustc_expand:: base:: { SyntaxExtension , SyntaxExtensionKind } ;
1818use rustc_expand:: compile_declarative_macro;
@@ -694,7 +694,25 @@ impl<'a> Resolver<'a> {
694694 check_consistency ( self , & path, path_span, kind, initial_res, res)
695695 }
696696 path_res @ PathResult :: NonModule ( ..) | path_res @ PathResult :: Failed { .. } => {
697+ let mut suggestion = None ;
697698 let ( span, label) = if let PathResult :: Failed { span, label, .. } = path_res {
699+ // try to suggest if it's not a macro, maybe a function
700+ if let PathResult :: NonModule ( partial_res) = self . resolve_path (
701+ & path,
702+ Some ( ValueNS ) ,
703+ & parent_scope,
704+ Some ( Finalize :: new ( ast:: CRATE_NODE_ID , path_span) ) ,
705+ None ,
706+ ) && partial_res. unresolved_segments ( ) == 0 {
707+ let sm = self . session . source_map ( ) ;
708+ let span = sm. span_extend_while ( span, |c| c == '!' ) . unwrap_or ( span) ;
709+ let code = sm. span_to_snippet ( span) . unwrap ( ) ;
710+ suggestion = Some (
711+ ( vec ! [ ( span, code. trim_end_matches( '!' ) . to_string( ) ) ] ,
712+ format ! ( "{} is not a macro, but a {}, try to remove `!`" , Segment :: names_to_string( & path) , partial_res. base_res( ) . descr( ) ) ,
713+ Applicability :: MaybeIncorrect )
714+ ) ;
715+ }
698716 ( span, label)
699717 } else {
700718 (
@@ -708,7 +726,7 @@ impl<'a> Resolver<'a> {
708726 } ;
709727 self . report_error (
710728 span,
711- ResolutionError :: FailedToResolve { label, suggestion : None } ,
729+ ResolutionError :: FailedToResolve { label, suggestion } ,
712730 ) ;
713731 }
714732 PathResult :: Module ( ..) | PathResult :: Indeterminate => unreachable ! ( ) ,
0 commit comments