Skip to content

Commit 0fe8ac3

Browse files
authored
Rollup merge of rust-lang#98782 - compiler-errors:specialization-error-span, r=oli-obk
Improve spans for specialization error Fixes rust-lang#98777
2 parents 6aace7f + a368830 commit 0fe8ac3

File tree

6 files changed

+31
-18
lines changed

6 files changed

+31
-18
lines changed

compiler/rustc_typeck/src/impl_wf_check/min_specialization.rs

+20-7
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,16 @@ fn check_predicates<'tcx>(
279279
span: Span,
280280
) {
281281
let tcx = infcx.tcx;
282-
let impl1_predicates: Vec<_> = traits::elaborate_predicates(
282+
let instantiated = tcx.predicates_of(impl1_def_id).instantiate(tcx, impl1_substs);
283+
let impl1_predicates: Vec<_> = traits::elaborate_predicates_with_span(
283284
tcx,
284-
tcx.predicates_of(impl1_def_id).instantiate(tcx, impl1_substs).predicates.into_iter(),
285+
std::iter::zip(
286+
instantiated.predicates,
287+
// Don't drop predicates (unsound!) because `spans` is too short
288+
instantiated.spans.into_iter().chain(std::iter::repeat(span)),
289+
),
285290
)
286-
.map(|obligation| obligation.predicate)
291+
.map(|obligation| (obligation.predicate, obligation.cause.span))
287292
.collect();
288293

289294
let mut impl2_predicates = if impl2_node.is_from_trait() {
@@ -321,7 +326,7 @@ fn check_predicates<'tcx>(
321326
// which is sound because we forbid impls like the following
322327
//
323328
// impl<D: Debug> AlwaysApplicable for D { }
324-
let always_applicable_traits = impl1_predicates.iter().copied().filter(|&predicate| {
329+
let always_applicable_traits = impl1_predicates.iter().copied().filter(|&(predicate, _)| {
325330
matches!(
326331
trait_predicate_kind(tcx, predicate),
327332
Some(TraitSpecializationKind::AlwaysApplicable)
@@ -345,11 +350,11 @@ fn check_predicates<'tcx>(
345350
}
346351
}
347352
impl2_predicates.extend(
348-
traits::elaborate_predicates(tcx, always_applicable_traits)
353+
traits::elaborate_predicates_with_span(tcx, always_applicable_traits)
349354
.map(|obligation| obligation.predicate),
350355
);
351356

352-
for predicate in impl1_predicates {
357+
for (predicate, span) in impl1_predicates {
353358
if !impl2_predicates.contains(&predicate) {
354359
check_specialization_on(tcx, predicate, span)
355360
}
@@ -384,9 +389,17 @@ fn check_specialization_on<'tcx>(tcx: TyCtxt<'tcx>, predicate: ty::Predicate<'tc
384389
.emit();
385390
}
386391
}
392+
ty::PredicateKind::Projection(ty::ProjectionPredicate { projection_ty, term }) => {
393+
tcx.sess
394+
.struct_span_err(
395+
span,
396+
&format!("cannot specialize on associated type `{projection_ty} == {term}`",),
397+
)
398+
.emit();
399+
}
387400
_ => {
388401
tcx.sess
389-
.struct_span_err(span, &format!("cannot specialize on `{:?}`", predicate))
402+
.struct_span_err(span, &format!("cannot specialize on predicate `{}`", predicate))
390403
.emit();
391404
}
392405
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: cannot specialize on `Binder(ProjectionPredicate(ProjectionTy { substs: [V], item_def_id: DefId(0:6 ~ repeated_projection_type[54ea]::Id::This) }, Ty((I,))), [])`
2-
--> $DIR/repeated_projection_type.rs:19:1
1+
error: cannot specialize on associated type `<V as Id>::This == (I,)`
2+
--> $DIR/repeated_projection_type.rs:19:15
33
|
44
LL | impl<I, V: Id<This = (I,)>> X for V {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^
66

77
error: aborting due to previous error
88

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: cannot specialize on trait `HasMethod`
2-
--> $DIR/spec-marker-supertraits.rs:22:1
2+
--> $DIR/spec-marker-supertraits.rs:22:9
33
|
44
LL | impl<T: Marker> Spec for T {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^
66

77
error: aborting due to previous error
88

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: cannot specialize on trait `Default`
2-
--> $DIR/specialization_super_trait.rs:13:1
2+
--> $DIR/specialization_super_trait.rs:13:9
33
|
44
LL | impl<T: Default> SpecMarker for T {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^
66

77
error: aborting due to previous error
88

src/test/ui/specialization/min_specialization/specialization_trait.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ LL | impl<T> SpecMarker for (T, T) {
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

1313
error: cannot specialize on trait `Clone`
14-
--> $DIR/specialization_trait.rs:21:1
14+
--> $DIR/specialization_trait.rs:21:9
1515
|
1616
LL | impl<T: Clone> SpecMarker for [T] {
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17+
| ^^^^^
1818

1919
error: aborting due to 3 previous errors
2020

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: cannot specialize on trait `SpecMarker`
2-
--> $DIR/specialize_on_trait.rs:15:1
2+
--> $DIR/specialize_on_trait.rs:15:9
33
|
44
LL | impl<T: SpecMarker> X for T {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^
66

77
error: aborting due to previous error
88

0 commit comments

Comments
 (0)