@@ -2945,16 +2945,53 @@ impl<'a> Resolver<'a> {
2945
2945
match * name_bindings. type_def . borrow ( ) {
2946
2946
None => { }
2947
2947
Some ( ref ty) => {
2948
- let msg = format ! ( "import `{}` conflicts with type in \
2949
- this module",
2950
- token:: get_name( name) . get( ) ) ;
2951
- self . session . span_err ( import_span, msg. as_slice ( ) ) ;
2952
- match ty. type_span {
2953
- None => { }
2954
- Some ( span) => {
2955
- self . session
2956
- . span_note ( span,
2957
- "conflicting type here" )
2948
+ match ty. module_def {
2949
+ None => {
2950
+ let msg = format ! ( "import `{}` conflicts with type in \
2951
+ this module",
2952
+ token:: get_name( name) . get( ) ) ;
2953
+ self . session . span_err ( import_span, msg. as_slice ( ) ) ;
2954
+ match ty. type_span {
2955
+ None => { }
2956
+ Some ( span) => {
2957
+ self . session
2958
+ . span_note ( span,
2959
+ "note conflicting type here" )
2960
+ }
2961
+ }
2962
+ }
2963
+ Some ( ref module_def) => {
2964
+ match module_def. kind . get ( ) {
2965
+ ImplModuleKind => {
2966
+ match ty. type_span {
2967
+ None => { /* this can't ever happen */ }
2968
+ Some ( span) => {
2969
+ let msg = format ! ( "inherent implementations \
2970
+ are only allowed on types \
2971
+ defined in the current module") ;
2972
+ self . session
2973
+ . span_err ( span, msg. as_slice ( ) ) ;
2974
+ self . session
2975
+ . span_note ( import_span,
2976
+ "import from other module here" )
2977
+ }
2978
+ }
2979
+ }
2980
+ _ => {
2981
+ let msg = format ! ( "import `{}` conflicts with existing \
2982
+ submodule",
2983
+ token:: get_name( name) . get( ) ) ;
2984
+ self . session . span_err ( import_span, msg. as_slice ( ) ) ;
2985
+ match ty. type_span {
2986
+ None => { }
2987
+ Some ( span) => {
2988
+ self . session
2989
+ . span_note ( span,
2990
+ "note conflicting module here" )
2991
+ }
2992
+ }
2993
+ }
2994
+ }
2958
2995
}
2959
2996
}
2960
2997
}
@@ -4610,6 +4647,30 @@ impl<'a> Resolver<'a> {
4610
4647
} ) ;
4611
4648
} ) ;
4612
4649
} ) ;
4650
+
4651
+ // Check that the current type is indeed a type, if we have an anonymous impl
4652
+ if opt_trait_reference. is_none ( ) {
4653
+ match self_type. node {
4654
+ // TyPath is the only thing that we handled in `build_reduced_graph_for_item`,
4655
+ // where we created a module with the name of the type in order to implement
4656
+ // an anonymous trait. In the case that the path does not resolve to an actual
4657
+ // type, the result will be that the type name resolves to a module but not
4658
+ // a type (shadowing any imported modules or types with this name), leading
4659
+ // to weird user-visible bugs. So we ward this off here. See #15060.
4660
+ TyPath ( ref path, _, path_id) => {
4661
+ match self . def_map . borrow ( ) . find ( & path_id) {
4662
+ // FIXME: should we catch other options and give more precise errors?
4663
+ Some ( & DefMod ( _) ) => {
4664
+ self . resolve_error ( path. span , "inherent implementations are not \
4665
+ allowed for types not defined in \
4666
+ the current module.") ;
4667
+ }
4668
+ _ => { }
4669
+ }
4670
+ }
4671
+ _ => { }
4672
+ }
4673
+ }
4613
4674
}
4614
4675
4615
4676
fn check_trait_item ( & self , ident : Ident , span : Span ) {
0 commit comments