@@ -1996,77 +1996,52 @@ impl<'a> Resolver<'a> {
19961996 }
19971997
19981998 fn resolve_type ( & mut self , ty : & Ty ) {
1999- match ty. node {
2000- TyKind :: Path ( ref maybe_qself, ref path) => {
2001- // This is a path in the type namespace. Walk through scopes
2002- // looking for it.
2003- if let Some ( def) = self . resolve_possibly_assoc_item ( ty. id , maybe_qself. as_ref ( ) ,
2004- path, TypeNS ) {
2005- match def. base_def {
2006- Def :: Mod ( ..) if def. depth == 0 => {
2007- self . session . span_err ( path. span , "expected type, found module" ) ;
2008- self . record_def ( ty. id , err_path_resolution ( ) ) ;
2009- }
2010- _ => {
2011- // Write the result into the def map.
2012- debug ! ( "(resolving type) writing resolution for `{}` (id {}) = {:?}" ,
2013- path_names_to_string( path, 0 ) , ty. id, def) ;
2014- self . record_def ( ty. id , def) ;
2015- }
1999+ if let TyKind :: Path ( ref maybe_qself, ref path) = ty. node {
2000+ // This is a path in the type namespace. Walk through scopes looking for it.
2001+ if let Some ( def) =
2002+ self . resolve_possibly_assoc_item ( ty. id , maybe_qself. as_ref ( ) , path, TypeNS ) {
2003+ match def. base_def {
2004+ Def :: Mod ( ..) if def. depth == 0 => {
2005+ self . session . span_err ( path. span , "expected type, found module" ) ;
2006+ self . record_def ( ty. id , err_path_resolution ( ) ) ;
20162007 }
2017- } else {
2018- self . record_def ( ty. id , err_path_resolution ( ) ) ;
2019-
2020- // Keep reporting some errors even if they're ignored above.
2021- {
2022- let kind = if maybe_qself. is_some ( ) {
2023- "associated type"
2024- } else {
2025- "type name"
2026- } ;
2027-
2028- let is_invalid_self_type_name = path. segments . len ( ) > 0 &&
2029- maybe_qself. is_none ( ) &&
2030- path. segments [ 0 ] . identifier . name ==
2031- keywords:: SelfType . name ( ) ;
2032- if is_invalid_self_type_name {
2033- resolve_error ( self ,
2034- ty. span ,
2035- ResolutionError :: SelfUsedOutsideImplOrTrait ) ;
2036- } else {
2037- let segment = path. segments . last ( ) ;
2038- let segment = segment. expect ( "missing name in path" ) ;
2039- let type_name = segment. identifier . name ;
2040-
2041- let candidates =
2042- self . lookup_candidates (
2043- type_name,
2044- TypeNS ,
2045- |def| match def {
2046- Def :: Trait ( _) |
2047- Def :: Enum ( _) |
2048- Def :: Struct ( _) |
2049- Def :: Union ( _) |
2050- Def :: TyAlias ( _) => true ,
2051- _ => false ,
2052- } ,
2053- ) ;
2054-
2055- // create error object
2056- let name = & path_names_to_string ( path, 0 ) ;
2057- let error =
2058- ResolutionError :: UseOfUndeclared (
2059- kind,
2060- name,
2061- candidates,
2062- ) ;
2008+ _ => {
2009+ // Write the result into the def map.
2010+ debug ! ( "(resolving type) writing resolution for `{}` (id {}) = {:?}" ,
2011+ path_names_to_string( path, 0 ) , ty. id, def) ;
2012+ self . record_def ( ty. id , def) ;
2013+ }
2014+ }
2015+ } else {
2016+ self . record_def ( ty. id , err_path_resolution ( ) ) ;
2017+ // Keep reporting some errors even if they're ignored above.
2018+ let kind = if maybe_qself. is_some ( ) { "associated type" } else { "type name" } ;
2019+ let is_invalid_self_type_name = {
2020+ path. segments . len ( ) > 0 &&
2021+ maybe_qself. is_none ( ) &&
2022+ path. segments [ 0 ] . identifier . name == keywords:: SelfType . name ( )
2023+ } ;
20632024
2064- resolve_error ( self , ty. span , error) ;
2025+ if is_invalid_self_type_name {
2026+ resolve_error ( self , ty. span , ResolutionError :: SelfUsedOutsideImplOrTrait ) ;
2027+ } else {
2028+ let type_name = path. segments . last ( ) . unwrap ( ) . identifier . name ;
2029+ let candidates = self . lookup_candidates ( type_name, TypeNS , |def| {
2030+ match def {
2031+ Def :: Trait ( _) |
2032+ Def :: Enum ( _) |
2033+ Def :: Struct ( _) |
2034+ Def :: Union ( _) |
2035+ Def :: TyAlias ( _) => true ,
2036+ _ => false ,
20652037 }
2066- }
2038+ } ) ;
2039+
2040+ let name = & path_names_to_string ( path, 0 ) ;
2041+ let error = ResolutionError :: UseOfUndeclared ( kind, name, candidates) ;
2042+ resolve_error ( self , ty. span , error) ;
20672043 }
20682044 }
2069- _ => { }
20702045 }
20712046 // Resolve embedded types.
20722047 visit:: walk_ty ( self , ty) ;
@@ -2697,78 +2672,72 @@ impl<'a> Resolver<'a> {
26972672 err. emit ( ) ;
26982673 } else {
26992674 // Keep reporting some errors even if they're ignored above.
2700- {
2701- let mut method_scope = false ;
2702- let mut is_static = false ;
2703- self . ribs [ ValueNS ] . iter ( ) . rev ( ) . all ( |rib| {
2704- method_scope = match rib. kind {
2705- MethodRibKind ( is_static_) => {
2706- is_static = is_static_;
2707- true
2708- }
2709- ItemRibKind | ConstantItemRibKind => false ,
2710- _ => return true , // Keep advancing
2711- } ;
2712- false // Stop advancing
2713- } ) ;
2675+ let mut method_scope = false ;
2676+ let mut is_static = false ;
2677+ self . ribs [ ValueNS ] . iter ( ) . rev ( ) . all ( |rib| {
2678+ method_scope = match rib. kind {
2679+ MethodRibKind ( is_static_) => {
2680+ is_static = is_static_;
2681+ true
2682+ }
2683+ ItemRibKind | ConstantItemRibKind => false ,
2684+ _ => return true , // Keep advancing
2685+ } ;
2686+ false // Stop advancing
2687+ } ) ;
27142688
2715- if method_scope && keywords:: SelfValue . name ( ) == & * path_name {
2716- resolve_error ( self ,
2717- expr. span ,
2718- ResolutionError :: SelfNotAvailableInStaticMethod ) ;
2719- } else {
2720- let last_name = path. last ( ) . unwrap ( ) . name ;
2721- let ( mut msg, is_field) =
2722- match self . find_fallback_in_self_type ( last_name) {
2723- NoSuggestion => {
2724- // limit search to 5 to reduce the number
2725- // of stupid suggestions
2726- ( match self . find_best_match ( & path_name) {
2727- SuggestionType :: Macro ( s) => {
2728- format ! ( "the macro `{}`" , s)
2729- }
2730- SuggestionType :: Function ( s) => format ! ( "`{}`" , s) ,
2731- SuggestionType :: NotFound => "" . to_string ( ) ,
2732- } , false )
2733- }
2734- Field => {
2735- ( if is_static && method_scope {
2736- "" . to_string ( )
2737- } else {
2738- format ! ( "`self.{}`" , path_name)
2739- } , true )
2740- }
2741- TraitItem => ( format ! ( "to call `self.{}`" , path_name) , false ) ,
2742- TraitMethod ( path_str) =>
2743- ( format ! ( "to call `{}::{}`" , path_str, path_name) , false ) ,
2744- } ;
2745-
2746- let mut context = UnresolvedNameContext :: Other ;
2747- let mut def = Def :: Err ;
2748- if !msg. is_empty ( ) {
2749- msg = format ! ( "did you mean {}?" , msg) ;
2750- } else {
2751- // we display a help message if this is a module
2752- match self . resolve_path ( & path, scope, None , None ) {
2753- PathResult :: Module ( module) => {
2754- def = module. def ( ) . unwrap ( ) ;
2755- context = UnresolvedNameContext :: PathIsMod ( parent) ;
2756- } ,
2757- _ => { } ,
2758- } ;
2689+ if method_scope && keywords:: SelfValue . name ( ) == & * path_name {
2690+ let error = ResolutionError :: SelfNotAvailableInStaticMethod ;
2691+ resolve_error ( self , expr. span , error) ;
2692+ } else {
2693+ let fallback =
2694+ self . find_fallback_in_self_type ( path. last ( ) . unwrap ( ) . name ) ;
2695+ let ( mut msg, is_field) = match fallback {
2696+ NoSuggestion => {
2697+ // limit search to 5 to reduce the number
2698+ // of stupid suggestions
2699+ ( match self . find_best_match ( & path_name) {
2700+ SuggestionType :: Macro ( s) => {
2701+ format ! ( "the macro `{}`" , s)
2702+ }
2703+ SuggestionType :: Function ( s) => format ! ( "`{}`" , s) ,
2704+ SuggestionType :: NotFound => "" . to_string ( ) ,
2705+ } , false )
2706+ }
2707+ Field => {
2708+ ( if is_static && method_scope {
2709+ "" . to_string ( )
2710+ } else {
2711+ format ! ( "`self.{}`" , path_name)
2712+ } , true )
27592713 }
2714+ TraitItem => ( format ! ( "to call `self.{}`" , path_name) , false ) ,
2715+ TraitMethod ( path_str) =>
2716+ ( format ! ( "to call `{}::{}`" , path_str, path_name) , false ) ,
2717+ } ;
27602718
2761- resolve_error ( self ,
2762- expr. span ,
2763- ResolutionError :: UnresolvedName {
2764- path : & path_name,
2765- message : & msg,
2766- context : context,
2767- is_static_method : method_scope && is_static,
2768- is_field : is_field,
2769- def : def,
2770- } ) ;
2719+ let mut context = UnresolvedNameContext :: Other ;
2720+ let mut def = Def :: Err ;
2721+ if !msg. is_empty ( ) {
2722+ msg = format ! ( "did you mean {}?" , msg) ;
2723+ } else {
2724+ // we display a help message if this is a module
2725+ if let PathResult :: Module ( module) =
2726+ self . resolve_path ( & path, scope, None , None ) {
2727+ def = module. def ( ) . unwrap ( ) ;
2728+ context = UnresolvedNameContext :: PathIsMod ( parent) ;
2729+ }
27712730 }
2731+
2732+ let error = ResolutionError :: UnresolvedName {
2733+ path : & path_name,
2734+ message : & msg,
2735+ context : context,
2736+ is_static_method : method_scope && is_static,
2737+ is_field : is_field,
2738+ def : def,
2739+ } ;
2740+ resolve_error ( self , expr. span , error) ;
27722741 }
27732742 }
27742743 }
0 commit comments