@@ -874,6 +874,10 @@ enum NameBindingKind<'a> {
874
874
binding : & ' a NameBinding < ' a > ,
875
875
directive : & ' a ImportDirective < ' a > ,
876
876
} ,
877
+ Ambiguity {
878
+ b1 : & ' a NameBinding < ' a > ,
879
+ b2 : & ' a NameBinding < ' a > ,
880
+ }
877
881
}
878
882
879
883
#[ derive( Clone , Debug ) ]
@@ -885,6 +889,7 @@ impl<'a> NameBinding<'a> {
885
889
NameBindingKind :: Module ( module) => Some ( module) ,
886
890
NameBindingKind :: Def ( _) => None ,
887
891
NameBindingKind :: Import { binding, .. } => binding. module ( ) ,
892
+ NameBindingKind :: Ambiguity { .. } => None ,
888
893
}
889
894
}
890
895
@@ -893,6 +898,7 @@ impl<'a> NameBinding<'a> {
893
898
NameBindingKind :: Def ( def) => def,
894
899
NameBindingKind :: Module ( module) => module. def . unwrap ( ) ,
895
900
NameBindingKind :: Import { binding, .. } => binding. def ( ) ,
901
+ NameBindingKind :: Ambiguity { .. } => Def :: Err ,
896
902
}
897
903
}
898
904
@@ -922,6 +928,7 @@ impl<'a> NameBinding<'a> {
922
928
fn is_glob_import ( & self ) -> bool {
923
929
match self . kind {
924
930
NameBindingKind :: Import { directive, .. } => directive. is_glob ( ) ,
931
+ NameBindingKind :: Ambiguity { .. } => true ,
925
932
_ => false ,
926
933
}
927
934
}
@@ -932,6 +939,14 @@ impl<'a> NameBinding<'a> {
932
939
_ => true ,
933
940
}
934
941
}
942
+
943
+ fn ambiguity ( & self ) -> Option < ( & ' a NameBinding < ' a > , & ' a NameBinding < ' a > ) > {
944
+ match self . kind {
945
+ NameBindingKind :: Ambiguity { b1, b2 } => Some ( ( b1, b2) ) ,
946
+ NameBindingKind :: Import { binding, .. } => binding. ambiguity ( ) ,
947
+ _ => None ,
948
+ }
949
+ }
935
950
}
936
951
937
952
/// Interns the names of the primitive types.
@@ -1249,7 +1264,8 @@ impl<'a> Resolver<'a> {
1249
1264
match ns { ValueNS => & mut self . value_ribs , TypeNS => & mut self . type_ribs }
1250
1265
}
1251
1266
1252
- fn record_use ( & mut self , name : Name , ns : Namespace , binding : & ' a NameBinding < ' a > ) {
1267
+ fn record_use ( & mut self , name : Name , ns : Namespace , binding : & ' a NameBinding < ' a > , span : Span )
1268
+ -> bool /* true if an error was reported */ {
1253
1269
// track extern crates for unused_extern_crate lint
1254
1270
if let Some ( DefId { krate, .. } ) = binding. module ( ) . and_then ( ModuleS :: def_id) {
1255
1271
self . used_crates . insert ( krate) ;
@@ -1259,6 +1275,19 @@ impl<'a> Resolver<'a> {
1259
1275
self . used_imports . insert ( ( directive. id , ns) ) ;
1260
1276
self . add_to_glob_map ( directive. id , name) ;
1261
1277
}
1278
+
1279
+ if let Some ( ( b1, b2) ) = binding. ambiguity ( ) {
1280
+ let msg1 = format ! ( "`{}` could resolve to the name imported here" , name) ;
1281
+ let msg2 = format ! ( "`{}` could also resolve to the name imported here" , name) ;
1282
+ self . session . struct_span_err ( span, & format ! ( "`{}` is ambiguous" , name) )
1283
+ . span_note ( b1. span , & msg1)
1284
+ . span_note ( b2. span , & msg2)
1285
+ . note ( & format ! ( "Consider adding an explicit import of `{}` to disambiguate" , name) )
1286
+ . emit ( ) ;
1287
+ return true ;
1288
+ }
1289
+
1290
+ false
1262
1291
}
1263
1292
1264
1293
fn add_to_glob_map ( & mut self , id : NodeId , name : Name ) {
@@ -2294,7 +2323,8 @@ impl<'a> Resolver<'a> {
2294
2323
Def :: Struct ( ..) | Def :: Variant ( ..) |
2295
2324
Def :: Const ( ..) | Def :: AssociatedConst ( ..) if !always_binding => {
2296
2325
// A constant, unit variant, etc pattern.
2297
- self . record_use ( ident. node . name , ValueNS , binding. unwrap ( ) ) ;
2326
+ let name = ident. node . name ;
2327
+ self . record_use ( name, ValueNS , binding. unwrap ( ) , ident. span ) ;
2298
2328
Some ( PathResolution :: new ( def) )
2299
2329
}
2300
2330
Def :: Struct ( ..) | Def :: Variant ( ..) |
0 commit comments