Compute a more precise span for opaque type impls
This commit is contained in:
parent
734adb3a1a
commit
08ef70bd13
@ -7,6 +7,7 @@ use rustc_errors::ErrorGuaranteed;
|
||||
use rustc_hir as hir;
|
||||
use rustc_index::bit_set::GrowableBitSet;
|
||||
use rustc_infer::infer::TyCtxtInferExt;
|
||||
use rustc_middle::ty::subst::GenericArgKind;
|
||||
use rustc_middle::ty::subst::{GenericArg, InternalSubsts};
|
||||
use rustc_middle::ty::{self, ImplPolarity, Ty, TyCtxt, TypeFoldable, TypeVisitor};
|
||||
use rustc_session::lint;
|
||||
@ -144,13 +145,41 @@ fn orphan_check_impl(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGua
|
||||
// Ensure no opaque types are present in this impl header. See issues #76202 and #86411 for examples,
|
||||
// and #84660 where it would otherwise allow unsoundness.
|
||||
if trait_ref.has_opaque_types() {
|
||||
trace!("{:#?}", item);
|
||||
for ty in trait_ref.substs {
|
||||
for ty in ty.walk() {
|
||||
let ty::subst::GenericArgKind::Type(ty) = ty.unpack() else { continue };
|
||||
let ty::Opaque(def_id, _) = ty.kind() else { continue };
|
||||
let ty::Opaque(def_id, _) = *ty.kind() else { continue };
|
||||
trace!(?def_id);
|
||||
struct SpanFinder<'tcx> {
|
||||
sp: Span,
|
||||
def_id: DefId,
|
||||
tcx: TyCtxt<'tcx>,
|
||||
}
|
||||
impl<'v, 'tcx> hir::intravisit::Visitor<'v> for SpanFinder<'tcx> {
|
||||
#[instrument(level = "trace", skip(self, _id))]
|
||||
fn visit_path(&mut self, path: &'v hir::Path<'v>, _id: hir::HirId) {
|
||||
if let hir::def::Res::Def(hir::def::DefKind::TyAlias, def_id) = path.res {
|
||||
for arg in self.tcx.type_of(def_id).walk() {
|
||||
if let GenericArgKind::Type(ty) = arg.unpack() {
|
||||
if let ty::Opaque(def_id, _) = *ty.kind() {
|
||||
if def_id == self.def_id {
|
||||
self.sp = path.span;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
hir::intravisit::walk_path(self, path)
|
||||
}
|
||||
}
|
||||
|
||||
let mut visitor = SpanFinder { sp, def_id, tcx };
|
||||
hir::intravisit::walk_item(&mut visitor, item);
|
||||
let reported = tcx
|
||||
.sess
|
||||
.struct_span_err(sp, "cannot implement trait on type alias impl trait")
|
||||
.struct_span_err(visitor.sp, "cannot implement trait on type alias impl trait")
|
||||
.span_note(tcx.def_span(def_id), "type alias impl trait defined here")
|
||||
.emit();
|
||||
return Err(reported);
|
||||
|
@ -1,8 +1,8 @@
|
||||
error: cannot implement trait on type alias impl trait
|
||||
--> $DIR/auto-trait.rs:21:1
|
||||
--> $DIR/auto-trait.rs:21:25
|
||||
|
|
||||
LL | impl AnotherTrait for D<OpaqueType> {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| ^^^^^^^^^^
|
||||
|
|
||||
note: type alias impl trait defined here
|
||||
--> $DIR/auto-trait.rs:7:19
|
||||
|
@ -1,8 +1,8 @@
|
||||
error: cannot implement trait on type alias impl trait
|
||||
--> $DIR/negative-reasoning.rs:19:1
|
||||
--> $DIR/negative-reasoning.rs:19:25
|
||||
|
|
||||
LL | impl AnotherTrait for D<OpaqueType> {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| ^^^^^^^^^^
|
||||
|
|
||||
note: type alias impl trait defined here
|
||||
--> $DIR/negative-reasoning.rs:7:19
|
||||
|
@ -1,8 +1,8 @@
|
||||
error: cannot implement trait on type alias impl trait
|
||||
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle-2.rs:7:1
|
||||
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle-2.rs:7:17
|
||||
|
|
||||
LL | impl PartialEq<(Foo, i32)> for Bar {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| ^^^
|
||||
|
|
||||
note: type alias impl trait defined here
|
||||
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle-2.rs:3:12
|
||||
|
@ -1,8 +1,8 @@
|
||||
error: cannot implement trait on type alias impl trait
|
||||
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:20:5
|
||||
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:20:21
|
||||
|
|
||||
LL | impl PartialEq<(Foo, i32)> for Bar {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| ^^^
|
||||
|
|
||||
note: type alias impl trait defined here
|
||||
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:16:16
|
||||
|
@ -1,8 +1,8 @@
|
||||
error: cannot implement trait on type alias impl trait
|
||||
--> $DIR/issue-83613.rs:10:1
|
||||
--> $DIR/issue-83613.rs:10:23
|
||||
|
|
||||
LL | impl AnotherTrait for OpaqueType {}
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| ^^^^^^^^^^
|
||||
|
|
||||
note: type alias impl trait defined here
|
||||
--> $DIR/issue-83613.rs:4:19
|
||||
|
@ -1,8 +1,8 @@
|
||||
error: cannot implement trait on type alias impl trait
|
||||
--> $DIR/issue-65384.rs:10:1
|
||||
--> $DIR/issue-65384.rs:10:18
|
||||
|
|
||||
LL | impl MyTrait for Bar {}
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
| ^^^
|
||||
|
|
||||
note: type alias impl trait defined here
|
||||
--> $DIR/issue-65384.rs:8:12
|
||||
|
@ -1,8 +1,8 @@
|
||||
error: cannot implement trait on type alias impl trait
|
||||
--> $DIR/issue-76202-trait-impl-for-tait.rs:16:1
|
||||
--> $DIR/issue-76202-trait-impl-for-tait.rs:16:15
|
||||
|
|
||||
LL | impl Test for F {
|
||||
| ^^^^^^^^^^^^^^^
|
||||
| ^
|
||||
|
|
||||
note: type alias impl trait defined here
|
||||
--> $DIR/issue-76202-trait-impl-for-tait.rs:9:10
|
||||
|
@ -1,8 +1,8 @@
|
||||
error: cannot implement trait on type alias impl trait
|
||||
--> $DIR/issue-84660-trait-impl-for-tait.rs:15:1
|
||||
--> $DIR/issue-84660-trait-impl-for-tait.rs:15:15
|
||||
|
|
||||
LL | impl TraitArg<Bar> for () {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| ^^^
|
||||
|
|
||||
note: type alias impl trait defined here
|
||||
--> $DIR/issue-84660-trait-impl-for-tait.rs:8:12
|
||||
|
@ -1,8 +1,8 @@
|
||||
error: cannot implement trait on type alias impl trait
|
||||
--> $DIR/issue-84660-unsoundness.rs:16:1
|
||||
--> $DIR/issue-84660-unsoundness.rs:16:21
|
||||
|
|
||||
LL | impl<In, Out> Trait<Bar, In> for Out {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
| ^^^
|
||||
|
|
||||
note: type alias impl trait defined here
|
||||
--> $DIR/issue-84660-unsoundness.rs:8:12
|
||||
|
@ -1,8 +1,8 @@
|
||||
error: cannot implement trait on type alias impl trait
|
||||
--> $DIR/nested-tait-inference3.rs:10:1
|
||||
--> $DIR/nested-tait-inference3.rs:10:10
|
||||
|
|
||||
LL | impl Foo<FooX> for () { }
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
| ^^^^
|
||||
|
|
||||
note: type alias impl trait defined here
|
||||
--> $DIR/nested-tait-inference3.rs:6:13
|
||||
|
Loading…
x
Reference in New Issue
Block a user