@@ -67,7 +67,7 @@ use syntax::ast::{Arm, BindingMode, Block, Crate, Expr, ExprKind};
67
67
use syntax:: ast:: { FnDecl , ForeignItem , ForeignItemKind , Generics } ;
68
68
use syntax:: ast:: { Item , ItemKind , ImplItem , ImplItemKind } ;
69
69
use syntax:: ast:: { Local , Pat , PatKind , Path } ;
70
- use syntax:: ast:: { PathSegment , PathParameters , TraitItemKind , TraitRef , Ty , TyKind } ;
70
+ use syntax:: ast:: { PathSegment , PathParameters , SelfKind , TraitItemKind , TraitRef , Ty , TyKind } ;
71
71
72
72
use std:: collections:: { HashMap , HashSet } ;
73
73
use std:: cell:: { Cell , RefCell } ;
@@ -148,7 +148,13 @@ enum ResolutionError<'a> {
148
148
/// error E0424: `self` is not available in a static method
149
149
SelfNotAvailableInStaticMethod ,
150
150
/// error E0425: unresolved name
151
- UnresolvedName ( & ' a str , & ' a str , UnresolvedNameContext < ' a > ) ,
151
+ UnresolvedName {
152
+ path : & ' a str ,
153
+ message : & ' a str ,
154
+ context : UnresolvedNameContext < ' a > ,
155
+ is_static_method : bool ,
156
+ is_field : bool
157
+ } ,
152
158
/// error E0426: use of undeclared label
153
159
UndeclaredLabel ( & ' a str ) ,
154
160
/// error E0427: cannot use `ref` binding mode with ...
@@ -406,16 +412,21 @@ fn resolve_struct_error<'b, 'a: 'b, 'c>(resolver: &'b Resolver<'a>,
406
412
"`self` is not available in a static method. Maybe a `self` \
407
413
argument is missing?")
408
414
}
409
- ResolutionError :: UnresolvedName ( path, msg, context) => {
415
+ ResolutionError :: UnresolvedName { path, message : msg, context, is_static_method,
416
+ is_field } => {
410
417
let mut err = struct_span_err ! ( resolver. session,
411
418
span,
412
419
E0425 ,
413
420
"unresolved name `{}`{}" ,
414
421
path,
415
422
msg) ;
416
-
417
423
match context {
418
- UnresolvedNameContext :: Other => { } // no help available
424
+ UnresolvedNameContext :: Other => {
425
+ if msg. is_empty ( ) && is_static_method && is_field {
426
+ err. help ( "this is an associated function, you don't have access to \
427
+ this type's fields or methods") ;
428
+ }
429
+ }
419
430
UnresolvedNameContext :: PathIsMod ( parent) => {
420
431
err. help ( & match parent. map ( |parent| & parent. node ) {
421
432
Some ( & ExprKind :: Field ( _, ident) ) => {
@@ -596,7 +607,7 @@ impl<'a, 'v> Visitor<'v> for Resolver<'a> {
596
607
}
597
608
FnKind :: Method ( _, sig, _) => {
598
609
self . visit_generics ( & sig. generics ) ;
599
- MethodRibKind
610
+ MethodRibKind ( sig . explicit_self . node == SelfKind :: Static )
600
611
}
601
612
FnKind :: Closure => ClosureRibKind ( node_id) ,
602
613
} ;
@@ -666,7 +677,9 @@ enum RibKind<'a> {
666
677
// methods. Allow references to ty params that impl or trait
667
678
// binds. Disallow any other upvars (including other ty params that are
668
679
// upvars).
669
- MethodRibKind ,
680
+ //
681
+ // The boolean value represents the fact that this method is static or not.
682
+ MethodRibKind ( bool ) ,
670
683
671
684
// We passed through an item scope. Disallow upvars.
672
685
ItemRibKind ,
@@ -1095,7 +1108,13 @@ impl<'a> hir::lowering::Resolver for Resolver<'a> {
1095
1108
Err ( false ) => {
1096
1109
let path_name = & format ! ( "{}" , path) ;
1097
1110
let error =
1098
- ResolutionError :: UnresolvedName ( path_name, "" , UnresolvedNameContext :: Other ) ;
1111
+ ResolutionError :: UnresolvedName {
1112
+ path : path_name,
1113
+ message : "" ,
1114
+ context : UnresolvedNameContext :: Other ,
1115
+ is_static_method : false ,
1116
+ is_field : false
1117
+ } ;
1099
1118
resolve_error ( self , path. span , error) ;
1100
1119
Def :: Err
1101
1120
}
@@ -1653,7 +1672,9 @@ impl<'a> Resolver<'a> {
1653
1672
let type_parameters =
1654
1673
HasTypeParameters ( & sig. generics ,
1655
1674
FnSpace ,
1656
- MethodRibKind ) ;
1675
+ MethodRibKind (
1676
+ sig. explicit_self . node ==
1677
+ SelfKind :: Static ) ) ;
1657
1678
this. with_type_parameter_rib ( type_parameters, |this| {
1658
1679
visit:: walk_trait_item ( this, trait_item)
1659
1680
} ) ;
@@ -1772,7 +1793,10 @@ impl<'a> Resolver<'a> {
1772
1793
self . value_ribs . pop ( ) ;
1773
1794
}
1774
1795
1775
- fn resolve_function ( & mut self , rib_kind : RibKind < ' a > , declaration : & FnDecl , block : & Block ) {
1796
+ fn resolve_function ( & mut self ,
1797
+ rib_kind : RibKind < ' a > ,
1798
+ declaration : & FnDecl ,
1799
+ block : & Block ) {
1776
1800
// Create a value rib for the function.
1777
1801
self . value_ribs . push ( Rib :: new ( rib_kind) ) ;
1778
1802
@@ -1979,7 +2003,9 @@ impl<'a> Resolver<'a> {
1979
2003
let type_parameters =
1980
2004
HasTypeParameters ( & sig. generics ,
1981
2005
FnSpace ,
1982
- MethodRibKind ) ;
2006
+ MethodRibKind (
2007
+ sig. explicit_self . node ==
2008
+ SelfKind :: Static ) ) ;
1983
2009
this. with_type_parameter_rib ( type_parameters, |this| {
1984
2010
visit:: walk_impl_item ( this, impl_item) ;
1985
2011
} ) ;
@@ -2673,7 +2699,7 @@ impl<'a> Resolver<'a> {
2673
2699
def = Def :: Upvar ( node_def_id, node_id, depth, function_id) ;
2674
2700
seen. insert ( node_id, depth) ;
2675
2701
}
2676
- ItemRibKind | MethodRibKind => {
2702
+ ItemRibKind | MethodRibKind ( _ ) => {
2677
2703
// This was an attempt to access an upvar inside a
2678
2704
// named function item. This is not allowed, so we
2679
2705
// report an error.
@@ -2695,7 +2721,7 @@ impl<'a> Resolver<'a> {
2695
2721
Def :: TyParam ( ..) | Def :: SelfTy ( ..) => {
2696
2722
for rib in ribs {
2697
2723
match rib. kind {
2698
- NormalRibKind | MethodRibKind | ClosureRibKind ( ..) |
2724
+ NormalRibKind | MethodRibKind ( _ ) | ClosureRibKind ( ..) |
2699
2725
ModuleRibKind ( ..) => {
2700
2726
// Nothing to do. Continue.
2701
2727
}
@@ -2988,9 +3014,13 @@ impl<'a> Resolver<'a> {
2988
3014
// `resolve_path` already reported the error
2989
3015
} else {
2990
3016
let mut method_scope = false ;
3017
+ let mut is_static = false ;
2991
3018
self . value_ribs . iter ( ) . rev ( ) . all ( |rib| {
2992
3019
method_scope = match rib. kind {
2993
- MethodRibKind => true ,
3020
+ MethodRibKind ( is_static_) => {
3021
+ is_static = is_static_;
3022
+ true
3023
+ }
2994
3024
ItemRibKind | ConstantItemRibKind => false ,
2995
3025
_ => return true , // Keep advancing
2996
3026
} ;
@@ -3004,22 +3034,29 @@ impl<'a> Resolver<'a> {
3004
3034
ResolutionError :: SelfNotAvailableInStaticMethod ) ;
3005
3035
} else {
3006
3036
let last_name = path. segments . last ( ) . unwrap ( ) . identifier . name ;
3007
- let mut msg = match self . find_fallback_in_self_type ( last_name) {
3037
+ let ( mut msg, is_field) =
3038
+ match self . find_fallback_in_self_type ( last_name) {
3008
3039
NoSuggestion => {
3009
3040
// limit search to 5 to reduce the number
3010
3041
// of stupid suggestions
3011
- match self . find_best_match ( & path_name) {
3042
+ ( match self . find_best_match ( & path_name) {
3012
3043
SuggestionType :: Macro ( s) => {
3013
3044
format ! ( "the macro `{}`" , s)
3014
3045
}
3015
3046
SuggestionType :: Function ( s) => format ! ( "`{}`" , s) ,
3016
3047
SuggestionType :: NotFound => "" . to_string ( ) ,
3017
- }
3048
+ } , false )
3049
+ }
3050
+ Field => {
3051
+ ( if is_static && method_scope {
3052
+ "" . to_string ( )
3053
+ } else {
3054
+ format ! ( "`self.{}`" , path_name)
3055
+ } , true )
3018
3056
}
3019
- Field => format ! ( "`self.{}`" , path_name) ,
3020
- TraitItem => format ! ( "to call `self.{}`" , path_name) ,
3057
+ TraitItem => ( format ! ( "to call `self.{}`" , path_name) , false ) ,
3021
3058
TraitMethod ( path_str) =>
3022
- format ! ( "to call `{}::{}`" , path_str, path_name) ,
3059
+ ( format ! ( "to call `{}::{}`" , path_str, path_name) , false ) ,
3023
3060
} ;
3024
3061
3025
3062
let mut context = UnresolvedNameContext :: Other ;
@@ -3044,8 +3081,13 @@ impl<'a> Resolver<'a> {
3044
3081
3045
3082
resolve_error ( self ,
3046
3083
expr. span ,
3047
- ResolutionError :: UnresolvedName (
3048
- & path_name, & msg, context) ) ;
3084
+ ResolutionError :: UnresolvedName {
3085
+ path : & path_name,
3086
+ message : & msg,
3087
+ context : context,
3088
+ is_static_method : method_scope && is_static,
3089
+ is_field : is_field,
3090
+ } ) ;
3049
3091
}
3050
3092
}
3051
3093
}
0 commit comments