22//!
33//! Module structure of the crate is built here.
44//! Paths in macros, imports, expressions, types, patterns are resolved here.
5- //! Label names are resolved here as well.
5+ //! Label and lifetime names are resolved here as well.
66//!
77//! Type-relative name resolution (methods, fields, associated items) happens in `librustc_typeck`.
8- //! Lifetime names are resolved in `librustc/middle/resolve_lifetime.rs`.
9-
10- // ignore-tidy-filelength
118
129#![ doc( html_root_url = "https://doc.rust-lang.org/nightly/" ) ]
1310#![ feature( bool_to_option) ]
@@ -33,7 +30,7 @@ use rustc_data_structures::sync::Lrc;
3330use rustc_errors:: { struct_span_err, Applicability , DiagnosticBuilder } ;
3431use rustc_expand:: base:: SyntaxExtension ;
3532use rustc_hir:: def:: Namespace :: * ;
36- use rustc_hir:: def:: { self , CtorKind , CtorOf , DefKind , NonMacroAttrKind , PartialRes } ;
33+ use rustc_hir:: def:: { self , CtorOf , DefKind , NonMacroAttrKind , PartialRes } ;
3734use rustc_hir:: def_id:: { CrateNum , DefId , DefIdMap , CRATE_DEF_INDEX , LOCAL_CRATE } ;
3835use rustc_hir:: PrimTy :: { self , Bool , Char , Float , Int , Str , Uint } ;
3936use rustc_hir:: { GlobMap , TraitMap } ;
@@ -604,7 +601,11 @@ impl<'a> NameBindingKind<'a> {
604601 }
605602}
606603
607- struct PrivacyError < ' a > ( Span , Ident , & ' a NameBinding < ' a > ) ;
604+ struct PrivacyError < ' a > {
605+ ident : Ident ,
606+ binding : & ' a NameBinding < ' a > ,
607+ dedup_span : Span ,
608+ }
608609
609610struct UseError < ' a > {
610611 err : DiagnosticBuilder < ' a > ,
@@ -2446,115 +2447,6 @@ impl<'a> Resolver<'a> {
24462447 }
24472448 }
24482449
2449- fn binding_description ( & self , b : & NameBinding < ' _ > , ident : Ident , from_prelude : bool ) -> String {
2450- let res = b. res ( ) ;
2451- if b. span . is_dummy ( ) {
2452- let add_built_in = match b. res ( ) {
2453- // These already contain the "built-in" prefix or look bad with it.
2454- Res :: NonMacroAttr ( ..) | Res :: PrimTy ( ..) | Res :: ToolMod => false ,
2455- _ => true ,
2456- } ;
2457- let ( built_in, from) = if from_prelude {
2458- ( "" , " from prelude" )
2459- } else if b. is_extern_crate ( )
2460- && !b. is_import ( )
2461- && self . session . opts . externs . get ( & ident. as_str ( ) ) . is_some ( )
2462- {
2463- ( "" , " passed with `--extern`" )
2464- } else if add_built_in {
2465- ( " built-in" , "" )
2466- } else {
2467- ( "" , "" )
2468- } ;
2469-
2470- let article = if built_in. is_empty ( ) { res. article ( ) } else { "a" } ;
2471- format ! (
2472- "{a}{built_in} {thing}{from}" ,
2473- a = article,
2474- thing = res. descr( ) ,
2475- built_in = built_in,
2476- from = from
2477- )
2478- } else {
2479- let introduced = if b. is_import ( ) { "imported" } else { "defined" } ;
2480- format ! ( "the {thing} {introduced} here" , thing = res. descr( ) , introduced = introduced)
2481- }
2482- }
2483-
2484- fn report_ambiguity_error ( & self , ambiguity_error : & AmbiguityError < ' _ > ) {
2485- let AmbiguityError { kind, ident, b1, b2, misc1, misc2 } = * ambiguity_error;
2486- let ( b1, b2, misc1, misc2, swapped) = if b2. span . is_dummy ( ) && !b1. span . is_dummy ( ) {
2487- // We have to print the span-less alternative first, otherwise formatting looks bad.
2488- ( b2, b1, misc2, misc1, true )
2489- } else {
2490- ( b1, b2, misc1, misc2, false )
2491- } ;
2492-
2493- let mut err = struct_span_err ! (
2494- self . session,
2495- ident. span,
2496- E0659 ,
2497- "`{ident}` is ambiguous ({why})" ,
2498- ident = ident,
2499- why = kind. descr( )
2500- ) ;
2501- err. span_label ( ident. span , "ambiguous name" ) ;
2502-
2503- let mut could_refer_to = |b : & NameBinding < ' _ > , misc : AmbiguityErrorMisc , also : & str | {
2504- let what = self . binding_description ( b, ident, misc == AmbiguityErrorMisc :: FromPrelude ) ;
2505- let note_msg = format ! (
2506- "`{ident}` could{also} refer to {what}" ,
2507- ident = ident,
2508- also = also,
2509- what = what
2510- ) ;
2511-
2512- let thing = b. res ( ) . descr ( ) ;
2513- let mut help_msgs = Vec :: new ( ) ;
2514- if b. is_glob_import ( )
2515- && ( kind == AmbiguityKind :: GlobVsGlob
2516- || kind == AmbiguityKind :: GlobVsExpanded
2517- || kind == AmbiguityKind :: GlobVsOuter && swapped != also. is_empty ( ) )
2518- {
2519- help_msgs. push ( format ! (
2520- "consider adding an explicit import of \
2521- `{ident}` to disambiguate",
2522- ident = ident
2523- ) )
2524- }
2525- if b. is_extern_crate ( ) && ident. span . rust_2018 ( ) {
2526- help_msgs. push ( format ! (
2527- "use `::{ident}` to refer to this {thing} unambiguously" ,
2528- ident = ident,
2529- thing = thing,
2530- ) )
2531- }
2532- if misc == AmbiguityErrorMisc :: SuggestCrate {
2533- help_msgs. push ( format ! (
2534- "use `crate::{ident}` to refer to this {thing} unambiguously" ,
2535- ident = ident,
2536- thing = thing,
2537- ) )
2538- } else if misc == AmbiguityErrorMisc :: SuggestSelf {
2539- help_msgs. push ( format ! (
2540- "use `self::{ident}` to refer to this {thing} unambiguously" ,
2541- ident = ident,
2542- thing = thing,
2543- ) )
2544- }
2545-
2546- err. span_note ( b. span , & note_msg) ;
2547- for ( i, help_msg) in help_msgs. iter ( ) . enumerate ( ) {
2548- let or = if i == 0 { "" } else { "or " } ;
2549- err. help ( & format ! ( "{}{}" , or, help_msg) ) ;
2550- }
2551- } ;
2552-
2553- could_refer_to ( b1, misc1, "" ) ;
2554- could_refer_to ( b2, misc2, " also" ) ;
2555- err. emit ( ) ;
2556- }
2557-
25582450 fn report_errors ( & mut self , krate : & Crate ) {
25592451 self . report_with_use_injections ( krate) ;
25602452
@@ -2575,43 +2467,9 @@ impl<'a> Resolver<'a> {
25752467 }
25762468
25772469 let mut reported_spans = FxHashSet :: default ( ) ;
2578- for & PrivacyError ( dedup_span, ident, binding) in & self . privacy_errors {
2579- if reported_spans. insert ( dedup_span) {
2580- let session = & self . session ;
2581- let mk_struct_span_error = |is_constructor| {
2582- struct_span_err ! (
2583- session,
2584- ident. span,
2585- E0603 ,
2586- "{}{} `{}` is private" ,
2587- binding. res( ) . descr( ) ,
2588- if is_constructor { " constructor" } else { "" } ,
2589- ident. name,
2590- )
2591- } ;
2592-
2593- let mut err = if let NameBindingKind :: Res (
2594- Res :: Def ( DefKind :: Ctor ( CtorOf :: Struct , CtorKind :: Fn ) , ctor_def_id) ,
2595- _,
2596- ) = binding. kind
2597- {
2598- let def_id = ( & * self ) . parent ( ctor_def_id) . expect ( "no parent for a constructor" ) ;
2599- if let Some ( fields) = self . field_names . get ( & def_id) {
2600- let mut err = mk_struct_span_error ( true ) ;
2601- let first_field = fields. first ( ) . expect ( "empty field list in the map" ) ;
2602- err. span_label (
2603- fields. iter ( ) . fold ( first_field. span , |acc, field| acc. to ( field. span ) ) ,
2604- "a constructor is private if any of the fields is private" ,
2605- ) ;
2606- err
2607- } else {
2608- mk_struct_span_error ( false )
2609- }
2610- } else {
2611- mk_struct_span_error ( false )
2612- } ;
2613-
2614- err. emit ( ) ;
2470+ for error in & self . privacy_errors {
2471+ if reported_spans. insert ( error. dedup_span ) {
2472+ self . report_privacy_error ( error) ;
26152473 }
26162474 }
26172475 }
0 commit comments