Skip to content

Commit 07d3044

Browse files
Defer error span calculation until needed
This was showing up in profiles. Also deduplicates the code to get just the impl header span.
1 parent f285239 commit 07d3044

File tree

1 file changed

+14
-5
lines changed
  • src/librustc_typeck/coherence

1 file changed

+14
-5
lines changed

src/librustc_typeck/coherence/mod.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ use rustc::ty::query::Providers;
1010
use rustc::ty::{self, TyCtxt, TypeFoldable};
1111
use rustc_errors::struct_span_err;
1212
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
13+
use rustc_span::Span;
1314

1415
mod builtin;
1516
mod inherent_impls;
1617
mod inherent_impls_overlap;
1718
mod orphan;
1819
mod unsafety;
1920

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+
2026
fn check_impl(tcx: TyCtxt<'_>, impl_def_id: DefId, trait_ref: ty::TraitRef<'_>) {
2127
debug!(
2228
"(checking implementation) adding impl for trait '{:?}', item '{}'",
@@ -37,10 +43,10 @@ fn check_impl(tcx: TyCtxt<'_>, impl_def_id: DefId, trait_ref: ty::TraitRef<'_>)
3743
fn enforce_trait_manually_implementable(tcx: TyCtxt<'_>, impl_def_id: DefId, trait_def_id: DefId) {
3844
let did = Some(trait_def_id);
3945
let li = tcx.lang_items();
40-
let span = tcx.sess.source_map().def_span(tcx.span_of_impl(impl_def_id).unwrap());
4146

4247
// Disallow *all* explicit impls of `Sized` and `Unsize` for now.
4348
if did == li.sized_trait() {
49+
let span = impl_header_span(tcx, impl_def_id);
4450
struct_span_err!(
4551
tcx.sess,
4652
span,
@@ -53,6 +59,7 @@ fn enforce_trait_manually_implementable(tcx: TyCtxt<'_>, impl_def_id: DefId, tra
5359
}
5460

5561
if did == li.unsize_trait() {
62+
let span = impl_header_span(tcx, impl_def_id);
5663
struct_span_err!(
5764
tcx.sess,
5865
span,
@@ -78,6 +85,8 @@ fn enforce_trait_manually_implementable(tcx: TyCtxt<'_>, impl_def_id: DefId, tra
7885
} else {
7986
return; // everything OK
8087
};
88+
89+
let span = impl_header_span(tcx, impl_def_id);
8190
struct_span_err!(
8291
tcx.sess,
8392
span,
@@ -101,7 +110,7 @@ fn enforce_empty_impls_for_marker_traits(tcx: TyCtxt<'_>, impl_def_id: DefId, tr
101110
return;
102111
}
103112

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);
105114
struct_span_err!(tcx.sess, span, E0715, "impls for marker traits cannot contain items").emit();
106115
}
107116

@@ -187,17 +196,17 @@ fn check_object_overlap<'tcx>(
187196
} else {
188197
let mut supertrait_def_ids = traits::supertrait_def_ids(tcx, component_def_id);
189198
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);
191200
struct_span_err!(
192201
tcx.sess,
193-
sp,
202+
span,
194203
E0371,
195204
"the object type `{}` automatically implements the trait `{}`",
196205
trait_ref.self_ty(),
197206
tcx.def_path_str(trait_def_id)
198207
)
199208
.span_label(
200-
sp,
209+
span,
201210
format!(
202211
"`{}` automatically implements trait `{}`",
203212
trait_ref.self_ty(),

0 commit comments

Comments
 (0)