Skip to content

Commit 351573e

Browse files
Do not rely on type_var_origin in OrphanCheckErr::NonLocalInputType
1 parent 2947be7 commit 351573e

File tree

5 files changed

+51
-58
lines changed

5 files changed

+51
-58
lines changed

compiler/rustc_hir_analysis/src/coherence/orphan.rs

+16-47
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
44
use rustc_data_structures::fx::FxIndexSet;
55
use rustc_errors::ErrorGuaranteed;
6-
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
6+
use rustc_infer::infer::{DefineOpaqueTypes, InferCtxt, TyCtxtInferExt};
77
use rustc_lint_defs::builtin::UNCOVERED_PARAM_IN_PROJECTION;
88
use rustc_middle::ty::{
9-
self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeSuperVisitable,
10-
TypeVisitable, TypeVisitableExt, TypeVisitor, TypingMode,
9+
self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, TypingMode,
1110
};
1211
use rustc_middle::{bug, span_bug};
1312
use rustc_span::def_id::{DefId, LocalDefId};
@@ -356,13 +355,20 @@ fn orphan_check<'tcx>(
356355
})
357356
}
358357
OrphanCheckErr::NonLocalInputType(tys) => {
359-
let generics = tcx.generics_of(impl_def_id);
360-
let tys = tys
361-
.into_iter()
362-
.map(|(ty, is_target_ty)| {
363-
(ty.fold_with(&mut TyVarReplacer { infcx: &infcx, generics }), is_target_ty)
364-
})
365-
.collect();
358+
let tys = infcx.probe(|_| {
359+
// Map the unconstrained args back to their params,
360+
// ignoring any type unification errors.
361+
for (arg, id_arg) in
362+
std::iter::zip(args, ty::GenericArgs::identity_for_item(tcx, impl_def_id))
363+
{
364+
let _ = infcx.at(&cause, ty::ParamEnv::empty()).eq(
365+
DefineOpaqueTypes::No,
366+
arg,
367+
id_arg,
368+
);
369+
}
370+
infcx.resolve_vars_if_possible(tys)
371+
});
366372
OrphanCheckErr::NonLocalInputType(tys)
367373
}
368374
})
@@ -536,40 +542,3 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for UncoveredTyParamCollector<'_, 'tcx> {
536542
}
537543
}
538544
}
539-
540-
struct TyVarReplacer<'cx, 'tcx> {
541-
infcx: &'cx InferCtxt<'tcx>,
542-
generics: &'tcx ty::Generics,
543-
}
544-
545-
impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for TyVarReplacer<'cx, 'tcx> {
546-
fn cx(&self) -> TyCtxt<'tcx> {
547-
self.infcx.tcx
548-
}
549-
550-
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
551-
if !ty.has_type_flags(ty::TypeFlags::HAS_TY_INFER) {
552-
return ty;
553-
}
554-
let ty::Infer(ty::TyVar(vid)) = *ty.kind() else {
555-
return ty.super_fold_with(self);
556-
};
557-
let origin = self.infcx.type_var_origin(vid);
558-
if let Some(def_id) = origin.param_def_id {
559-
// The generics of an `impl` don't have a parent, we can index directly.
560-
let index = self.generics.param_def_id_to_index[&def_id];
561-
let name = self.generics.own_params[index as usize].name;
562-
563-
Ty::new_param(self.infcx.tcx, index, name)
564-
} else {
565-
ty
566-
}
567-
}
568-
569-
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
570-
if !ct.has_type_flags(ty::TypeFlags::HAS_TY_INFER) {
571-
return ct;
572-
}
573-
ct.super_fold_with(self)
574-
}
575-
}

compiler/rustc_next_trait_solver/src/coherence.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_type_ir::inherent::*;
66
use rustc_type_ir::{
77
self as ty, InferCtxtLike, Interner, TypeVisitable, TypeVisitableExt, TypeVisitor,
88
};
9+
use rustc_type_ir_macros::{TypeFoldable_Generic, TypeVisitable_Generic};
910
use tracing::instrument;
1011

1112
/// Whether we do the orphan check relative to this crate or to some remote crate.
@@ -95,7 +96,7 @@ pub fn trait_ref_is_local_or_fundamental<I: Interner>(tcx: I, trait_ref: ty::Tra
9596
trait_ref.def_id.is_local() || tcx.trait_is_fundamental(trait_ref.def_id)
9697
}
9798

98-
#[derive(Debug, Copy, Clone)]
99+
#[derive(Debug, Copy, Clone, TypeFoldable_Generic, TypeVisitable_Generic)]
99100
pub enum IsFirstInputType {
100101
No,
101102
Yes,

tests/crashes/132826.rs

-10
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Regression test for #132826.
2+
3+
// Make sure we don't try to resolve the variable `K1` in the generics of the impl
4+
// (which only has `K2`).
5+
6+
pub trait MyTrait {
7+
type Item;
8+
}
9+
10+
impl<K1> MyTrait for Vec<K1> {
11+
type Item = Vec<K1>;
12+
}
13+
14+
impl<K2> From<Vec<K2>> for <Vec<K2> as MyTrait>::Item {}
15+
//~^ ERROR only traits defined in the current crate can be implemented for arbitrary types
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
2+
--> $DIR/orphan-check-error-reporting-ty-var.rs:14:1
3+
|
4+
LL | impl<K2> From<Vec<K2>> for <Vec<K2> as MyTrait>::Item {}
5+
| ^^^^^^^^^-------------^^^^^--------------------------
6+
| | |
7+
| | `Vec` is not defined in the current crate
8+
| `Vec` is not defined in the current crate
9+
|
10+
= note: impl doesn't have any local type before any uncovered type parameters
11+
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
12+
= note: define and implement a trait or new type instead
13+
14+
error: aborting due to 1 previous error
15+
16+
For more information about this error, try `rustc --explain E0117`.

0 commit comments

Comments
 (0)