@@ -37,7 +37,7 @@ use crate::ref_mut::CmCell;
3737use crate :: {
3838 BindingKey , Decl , DeclData , DeclKind , ExternModule , ExternPreludeEntry , Finalize , IdentKey ,
3939 LocalModule , MacroData , Module , ModuleKind , ModuleOrUniformRoot , ParentScope , PathResult , Res ,
40- ResolutionError , Resolver , Segment , Used , VisResolutionError , errors,
40+ Resolver , Segment , Used , VisResolutionError , errors,
4141} ;
4242
4343impl < ' ra , ' tcx > Resolver < ' ra , ' tcx > {
@@ -531,18 +531,16 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
531531
532532 self . r . indeterminate_imports . push ( import) ;
533533 match import. kind {
534- ImportKind :: Single { target, type_ns_only , .. } => {
534+ ImportKind :: Single { target, .. } => {
535535 // Don't add underscore imports to `single_imports`
536536 // because they cannot define any usable names.
537537 if target. name != kw:: Underscore {
538538 self . r . per_ns ( |this, ns| {
539- if !type_ns_only || ns == TypeNS {
540- let key = BindingKey :: new ( IdentKey :: new ( target) , ns) ;
541- this. resolution_or_default ( current_module, key, target. span )
542- . borrow_mut ( this)
543- . single_imports
544- . insert ( import) ;
545- }
539+ let key = BindingKey :: new ( IdentKey :: new ( target) , ns) ;
540+ this. resolution_or_default ( current_module, key, target. span )
541+ . borrow_mut ( this)
542+ . single_imports
543+ . insert ( import) ;
546544 } ) ;
547545 }
548546 }
@@ -612,30 +610,8 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
612610 let mut module_path = prefix;
613611 let source = module_path. pop ( ) . unwrap ( ) ;
614612
615- // `true` for `...::{self [as target]}` imports, `false` otherwise.
616- let type_ns_only = nested && source. ident . name == kw:: SelfLower ;
617-
618- // Suggest `use prefix::{self};` for `use prefix::self;`
619- if source. ident . name == kw:: SelfLower
620- && let Some ( parent) = module_path. last ( )
621- && !type_ns_only
622- && ( parent. ident . name != kw:: PathRoot
623- || self . r . path_root_is_crate_root ( parent. ident ) )
624- {
625- let span_with_rename = match rename {
626- Some ( rename) => source. ident . span . to ( rename. span ) ,
627- None => source. ident . span ,
628- } ;
629-
630- self . r . report_error (
631- parent. ident . span . shrink_to_hi ( ) . to ( source. ident . span ) ,
632- ResolutionError :: SelfImportsOnlyAllowedWithin {
633- root : parent. ident . name == kw:: PathRoot ,
634- span_with_rename,
635- } ,
636- ) ;
637- }
638-
613+ // If the identifier is `self` without a rename,
614+ // then it is replaced with the parent identifier.
639615 let ident = if source. ident . name == kw:: SelfLower
640616 && rename. is_none ( )
641617 && let Some ( parent) = module_path. last ( )
@@ -689,30 +665,36 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
689665 self . r . dcx ( ) . span_err ( use_tree. span ( ) , "extern prelude cannot be imported" ) ;
690666 return ;
691667 }
692- _ => { }
668+ _ => ( ) ,
669+ }
670+
671+ // Deny `use ...::self::source [as target];` or `use ...::self::self [as target];`,
672+ // but allow `use self::source [as target];` and `use self::self as target;`.
673+ if let Some ( parent) = module_path. last ( )
674+ && parent. ident . name == kw:: SelfLower
675+ && module_path. len ( ) > 1
676+ {
677+ self . r . dcx ( ) . span_err (
678+ parent. ident . span ,
679+ "`self` in paths can only be used in start position or last position" ,
680+ ) ;
681+ return ;
693682 }
694683
695684 // Deny importing path-kw without renaming
696685 if rename. is_none ( ) && ident. is_path_segment_keyword ( ) {
697686 let ident = use_tree. ident ( ) ;
698-
699- // Don't suggest `use xx::self as name;` for `use xx::self;`
700- // But it's OK to suggest `use xx::{self as name};` for `use xx::{self};`
701- let sugg = if !type_ns_only && ident. name == kw:: SelfLower {
702- None
703- } else {
704- Some ( errors:: UnnamedImportSugg { span : ident. span , ident } )
705- } ;
706-
707- self . r . dcx ( ) . emit_err ( errors:: UnnamedImport { span : ident. span , sugg } ) ;
687+ self . r . dcx ( ) . emit_err ( errors:: UnnamedImport {
688+ span : ident. span ,
689+ sugg : errors:: UnnamedImportSugg { span : ident. span , ident } ,
690+ } ) ;
708691 return ;
709692 }
710693
711694 let kind = ImportKind :: Single {
712695 source : source. ident ,
713696 target : ident,
714697 decls : Default :: default ( ) ,
715- type_ns_only,
716698 nested,
717699 id,
718700 } ;
0 commit comments