@@ -10,13 +10,19 @@ use rustc::ty::query::Providers;
10
10
use rustc:: ty:: { self , TyCtxt , TypeFoldable } ;
11
11
use rustc_errors:: struct_span_err;
12
12
use rustc_hir:: def_id:: { DefId , LOCAL_CRATE } ;
13
+ use rustc_span:: Span ;
13
14
14
15
mod builtin;
15
16
mod inherent_impls;
16
17
mod inherent_impls_overlap;
17
18
mod orphan;
18
19
mod unsafety;
19
20
21
+ /// Obtains the span of just the impl header of `impl_def_id`.
22
+ fn impl_header_span ( tcx : TyCtxt < ' _ > , impl_def_id : DefId ) -> Span {
23
+ tcx. sess . source_map ( ) . def_span ( tcx. span_of_impl ( impl_def_id) . unwrap ( ) )
24
+ }
25
+
20
26
fn check_impl ( tcx : TyCtxt < ' _ > , impl_def_id : DefId , trait_ref : ty:: TraitRef < ' _ > ) {
21
27
debug ! (
22
28
"(checking implementation) adding impl for trait '{:?}', item '{}'" ,
@@ -37,10 +43,10 @@ fn check_impl(tcx: TyCtxt<'_>, impl_def_id: DefId, trait_ref: ty::TraitRef<'_>)
37
43
fn enforce_trait_manually_implementable ( tcx : TyCtxt < ' _ > , impl_def_id : DefId , trait_def_id : DefId ) {
38
44
let did = Some ( trait_def_id) ;
39
45
let li = tcx. lang_items ( ) ;
40
- let span = tcx. sess . source_map ( ) . def_span ( tcx. span_of_impl ( impl_def_id) . unwrap ( ) ) ;
41
46
42
47
// Disallow *all* explicit impls of `Sized` and `Unsize` for now.
43
48
if did == li. sized_trait ( ) {
49
+ let span = impl_header_span ( tcx, impl_def_id) ;
44
50
struct_span_err ! (
45
51
tcx. sess,
46
52
span,
@@ -53,6 +59,7 @@ fn enforce_trait_manually_implementable(tcx: TyCtxt<'_>, impl_def_id: DefId, tra
53
59
}
54
60
55
61
if did == li. unsize_trait ( ) {
62
+ let span = impl_header_span ( tcx, impl_def_id) ;
56
63
struct_span_err ! (
57
64
tcx. sess,
58
65
span,
@@ -78,6 +85,8 @@ fn enforce_trait_manually_implementable(tcx: TyCtxt<'_>, impl_def_id: DefId, tra
78
85
} else {
79
86
return ; // everything OK
80
87
} ;
88
+
89
+ let span = impl_header_span ( tcx, impl_def_id) ;
81
90
struct_span_err ! (
82
91
tcx. sess,
83
92
span,
@@ -101,7 +110,7 @@ fn enforce_empty_impls_for_marker_traits(tcx: TyCtxt<'_>, impl_def_id: DefId, tr
101
110
return ;
102
111
}
103
112
104
- let span = tcx . sess . source_map ( ) . def_span ( tcx. span_of_impl ( impl_def_id) . unwrap ( ) ) ;
113
+ let span = impl_header_span ( tcx, impl_def_id) ;
105
114
struct_span_err ! ( tcx. sess, span, E0715 , "impls for marker traits cannot contain items" ) . emit ( ) ;
106
115
}
107
116
@@ -187,17 +196,17 @@ fn check_object_overlap<'tcx>(
187
196
} else {
188
197
let mut supertrait_def_ids = traits:: supertrait_def_ids ( tcx, component_def_id) ;
189
198
if supertrait_def_ids. any ( |d| d == trait_def_id) {
190
- let sp = tcx . sess . source_map ( ) . def_span ( tcx. span_of_impl ( impl_def_id) . unwrap ( ) ) ;
199
+ let span = impl_header_span ( tcx, impl_def_id) ;
191
200
struct_span_err ! (
192
201
tcx. sess,
193
- sp ,
202
+ span ,
194
203
E0371 ,
195
204
"the object type `{}` automatically implements the trait `{}`" ,
196
205
trait_ref. self_ty( ) ,
197
206
tcx. def_path_str( trait_def_id)
198
207
)
199
208
. span_label (
200
- sp ,
209
+ span ,
201
210
format ! (
202
211
"`{}` automatically implements trait `{}`" ,
203
212
trait_ref. self_ty( ) ,
0 commit comments