about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2022-04-12 12:28:31 +0000
committerOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2022-04-12 12:28:31 +0000
commit08ef70bd1398efd882b985959baaa4a5cc6dd738 (patch)
treefdadcab4f7c3b6a91158c62844b183e655dcd11f /compiler
parent734adb3a1a8890863826fd1ce75823d104e33a64 (diff)
downloadrust-08ef70bd1398efd882b985959baaa4a5cc6dd738.tar.gz
rust-08ef70bd1398efd882b985959baaa4a5cc6dd738.zip
Compute a more precise span for opaque type impls
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_typeck/src/coherence/orphan.rs33
1 files changed, 31 insertions, 2 deletions
diff --git a/compiler/rustc_typeck/src/coherence/orphan.rs b/compiler/rustc_typeck/src/coherence/orphan.rs
index 564c3d31368..76c41dedf0a 100644
--- a/compiler/rustc_typeck/src/coherence/orphan.rs
+++ b/compiler/rustc_typeck/src/coherence/orphan.rs
@@ -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);