Skip to content

Commit 08ef70b

Browse files
committed
Compute a more precise span for opaque type impls
1 parent 734adb3 commit 08ef70b

11 files changed

+51
-22
lines changed

compiler/rustc_typeck/src/coherence/orphan.rs

+31-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_errors::ErrorGuaranteed;
77
use rustc_hir as hir;
88
use rustc_index::bit_set::GrowableBitSet;
99
use rustc_infer::infer::TyCtxtInferExt;
10+
use rustc_middle::ty::subst::GenericArgKind;
1011
use rustc_middle::ty::subst::{GenericArg, InternalSubsts};
1112
use rustc_middle::ty::{self, ImplPolarity, Ty, TyCtxt, TypeFoldable, TypeVisitor};
1213
use rustc_session::lint;
@@ -144,13 +145,41 @@ fn orphan_check_impl(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGua
144145
// Ensure no opaque types are present in this impl header. See issues #76202 and #86411 for examples,
145146
// and #84660 where it would otherwise allow unsoundness.
146147
if trait_ref.has_opaque_types() {
148+
trace!("{:#?}", item);
147149
for ty in trait_ref.substs {
148150
for ty in ty.walk() {
149151
let ty::subst::GenericArgKind::Type(ty) = ty.unpack() else { continue };
150-
let ty::Opaque(def_id, _) = ty.kind() else { continue };
152+
let ty::Opaque(def_id, _) = *ty.kind() else { continue };
153+
trace!(?def_id);
154+
struct SpanFinder<'tcx> {
155+
sp: Span,
156+
def_id: DefId,
157+
tcx: TyCtxt<'tcx>,
158+
}
159+
impl<'v, 'tcx> hir::intravisit::Visitor<'v> for SpanFinder<'tcx> {
160+
#[instrument(level = "trace", skip(self, _id))]
161+
fn visit_path(&mut self, path: &'v hir::Path<'v>, _id: hir::HirId) {
162+
if let hir::def::Res::Def(hir::def::DefKind::TyAlias, def_id) = path.res {
163+
for arg in self.tcx.type_of(def_id).walk() {
164+
if let GenericArgKind::Type(ty) = arg.unpack() {
165+
if let ty::Opaque(def_id, _) = *ty.kind() {
166+
if def_id == self.def_id {
167+
self.sp = path.span;
168+
return;
169+
}
170+
}
171+
}
172+
}
173+
}
174+
hir::intravisit::walk_path(self, path)
175+
}
176+
}
177+
178+
let mut visitor = SpanFinder { sp, def_id, tcx };
179+
hir::intravisit::walk_item(&mut visitor, item);
151180
let reported = tcx
152181
.sess
153-
.struct_span_err(sp, "cannot implement trait on type alias impl trait")
182+
.struct_span_err(visitor.sp, "cannot implement trait on type alias impl trait")
154183
.span_note(tcx.def_span(def_id), "type alias impl trait defined here")
155184
.emit();
156185
return Err(reported);

src/test/ui/impl-trait/auto-trait.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: cannot implement trait on type alias impl trait
2-
--> $DIR/auto-trait.rs:21:1
2+
--> $DIR/auto-trait.rs:21:25
33
|
44
LL | impl AnotherTrait for D<OpaqueType> {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^
66
|
77
note: type alias impl trait defined here
88
--> $DIR/auto-trait.rs:7:19

src/test/ui/impl-trait/negative-reasoning.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: cannot implement trait on type alias impl trait
2-
--> $DIR/negative-reasoning.rs:19:1
2+
--> $DIR/negative-reasoning.rs:19:25
33
|
44
LL | impl AnotherTrait for D<OpaqueType> {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^
66
|
77
note: type alias impl trait defined here
88
--> $DIR/negative-reasoning.rs:7:19

src/test/ui/impl-trait/recursive-type-alias-impl-trait-declaration-too-subtle-2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: cannot implement trait on type alias impl trait
2-
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle-2.rs:7:1
2+
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle-2.rs:7:17
33
|
44
LL | impl PartialEq<(Foo, i32)> for Bar {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^
66
|
77
note: type alias impl trait defined here
88
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle-2.rs:3:12

src/test/ui/impl-trait/recursive-type-alias-impl-trait-declaration-too-subtle.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: cannot implement trait on type alias impl trait
2-
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:20:5
2+
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:20:21
33
|
44
LL | impl PartialEq<(Foo, i32)> for Bar {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^
66
|
77
note: type alias impl trait defined here
88
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:16:16

src/test/ui/traits/alias/issue-83613.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: cannot implement trait on type alias impl trait
2-
--> $DIR/issue-83613.rs:10:1
2+
--> $DIR/issue-83613.rs:10:23
33
|
44
LL | impl AnotherTrait for OpaqueType {}
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^
66
|
77
note: type alias impl trait defined here
88
--> $DIR/issue-83613.rs:4:19

src/test/ui/type-alias-impl-trait/issue-65384.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: cannot implement trait on type alias impl trait
2-
--> $DIR/issue-65384.rs:10:1
2+
--> $DIR/issue-65384.rs:10:18
33
|
44
LL | impl MyTrait for Bar {}
5-
| ^^^^^^^^^^^^^^^^^^^^
5+
| ^^^
66
|
77
note: type alias impl trait defined here
88
--> $DIR/issue-65384.rs:8:12

src/test/ui/type-alias-impl-trait/issue-76202-trait-impl-for-tait.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: cannot implement trait on type alias impl trait
2-
--> $DIR/issue-76202-trait-impl-for-tait.rs:16:1
2+
--> $DIR/issue-76202-trait-impl-for-tait.rs:16:15
33
|
44
LL | impl Test for F {
5-
| ^^^^^^^^^^^^^^^
5+
| ^
66
|
77
note: type alias impl trait defined here
88
--> $DIR/issue-76202-trait-impl-for-tait.rs:9:10

src/test/ui/type-alias-impl-trait/issue-84660-trait-impl-for-tait.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: cannot implement trait on type alias impl trait
2-
--> $DIR/issue-84660-trait-impl-for-tait.rs:15:1
2+
--> $DIR/issue-84660-trait-impl-for-tait.rs:15:15
33
|
44
LL | impl TraitArg<Bar> for () {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^
66
|
77
note: type alias impl trait defined here
88
--> $DIR/issue-84660-trait-impl-for-tait.rs:8:12

src/test/ui/type-alias-impl-trait/issue-84660-unsoundness.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: cannot implement trait on type alias impl trait
2-
--> $DIR/issue-84660-unsoundness.rs:16:1
2+
--> $DIR/issue-84660-unsoundness.rs:16:21
33
|
44
LL | impl<In, Out> Trait<Bar, In> for Out {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^
66
|
77
note: type alias impl trait defined here
88
--> $DIR/issue-84660-unsoundness.rs:8:12

src/test/ui/type-alias-impl-trait/nested-tait-inference3.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: cannot implement trait on type alias impl trait
2-
--> $DIR/nested-tait-inference3.rs:10:1
2+
--> $DIR/nested-tait-inference3.rs:10:10
33
|
44
LL | impl Foo<FooX> for () { }
5-
| ^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^
66
|
77
note: type alias impl trait defined here
88
--> $DIR/nested-tait-inference3.rs:6:13

0 commit comments

Comments
 (0)