mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 14:13:58 +00:00
Auto merge of #17882 - ShoyuVanilla:issue-17866, r=lnicola
fix: Panic while canonicalizing erroneous projection type Fixes #17866 The root cause of #17866 is quite horrifyng 😨 ```rust trait T { type A; } type Foo = <S as T>::A; // note that S isn't defined fn main() { Foo {} } ``` While inferencing alias type `Foo = <S as T>::A`;78c2bdce86/crates/hir-ty/src/infer.rs (L1388-L1398)
the error type `S` in it is substituted by inference var in L1396 above as below;78c2bdce86/crates/hir-ty/src/infer/unify.rs (L866-L869)
This new inference var's index is `1`, as the type inferecing procedure here previously inserted another inference var into same `InferenceTable`. But after that, the projection type made from the above then passed to the following function;78c2bdce86/crates/hir-ty/src/traits.rs (L88-L96)
here, a whole new `InferenceTable` is made, without any inference var and in the L94, this table calls;78c2bdce86/crates/hir-ty/src/infer/unify.rs (L364-L370)
And while registering `AliasEq` `obligation`, this obligation contains inference var `?1` made from the previous table, but this table has only one inference var `?0` made at L365. So, the chalk panics when we try to canonicalize that obligation to register it, because the obligation contains an inference var `?1` that the canonicalizing table doesn't have. Currently, we are calling `InferenceTable::new()` to do some normalizing, unifying or coercing things to some targets that might contain inference var that the new table doesn't have. I think that this is quite dangerous footgun because the inference var is just an index that does not contain the information which table does it made from, so sometimes this "foreign" index might cause panic like this case, or point at the wrong variable. This PR mitigates such behaviour simply by inserting sufficient number of inference vars to new table to avoid such problem. This strategy doesn't harm current r-a's intention because the inference vars that passed into new tables are just "unresolved" variables in current r-a, so this is just making sure that such "unresolved" variables exist in the new table
This commit is contained in:
commit
64a140527b
3 changed files with 35 additions and 3 deletions
|
@ -1436,7 +1436,8 @@ impl<'a> InferenceContext<'a> {
|
||||||
let remaining = unresolved.map(|it| path.segments()[it..].len()).filter(|it| it > &0);
|
let remaining = unresolved.map(|it| path.segments()[it..].len()).filter(|it| it > &0);
|
||||||
let ty = match ty.kind(Interner) {
|
let ty = match ty.kind(Interner) {
|
||||||
TyKind::Alias(AliasTy::Projection(proj_ty)) => {
|
TyKind::Alias(AliasTy::Projection(proj_ty)) => {
|
||||||
self.db.normalize_projection(proj_ty.clone(), self.table.trait_env.clone())
|
let ty = self.table.normalize_projection_ty(proj_ty.clone());
|
||||||
|
self.table.resolve_ty_shallow(&ty)
|
||||||
}
|
}
|
||||||
_ => ty,
|
_ => ty,
|
||||||
};
|
};
|
||||||
|
|
|
@ -2141,3 +2141,24 @@ fn test() {
|
||||||
}"#,
|
}"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn issue_17866() {
|
||||||
|
check_infer(
|
||||||
|
r#"
|
||||||
|
trait T {
|
||||||
|
type A;
|
||||||
|
}
|
||||||
|
|
||||||
|
type Foo = <S as T>::A;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
Foo {};
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
expect![[r#"
|
||||||
|
60..75 '{ Foo {}; }': ()
|
||||||
|
66..72 'Foo {}': {unknown}
|
||||||
|
"#]],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
@ -14,13 +14,13 @@ use hir_def::{
|
||||||
};
|
};
|
||||||
use hir_expand::name::Name;
|
use hir_expand::name::Name;
|
||||||
use intern::sym;
|
use intern::sym;
|
||||||
use stdx::panic_context;
|
use stdx::{never, panic_context};
|
||||||
use triomphe::Arc;
|
use triomphe::Arc;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::HirDatabase, infer::unify::InferenceTable, utils::UnevaluatedConstEvaluatorFolder, AliasEq,
|
db::HirDatabase, infer::unify::InferenceTable, utils::UnevaluatedConstEvaluatorFolder, AliasEq,
|
||||||
AliasTy, Canonical, DomainGoal, Goal, Guidance, InEnvironment, Interner, ProjectionTy,
|
AliasTy, Canonical, DomainGoal, Goal, Guidance, InEnvironment, Interner, ProjectionTy,
|
||||||
ProjectionTyExt, Solution, TraitRefExt, Ty, TyKind, WhereClause,
|
ProjectionTyExt, Solution, TraitRefExt, Ty, TyKind, TypeFlags, WhereClause,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// This controls how much 'time' we give the Chalk solver before giving up.
|
/// This controls how much 'time' we give the Chalk solver before giving up.
|
||||||
|
@ -90,6 +90,16 @@ pub(crate) fn normalize_projection_query(
|
||||||
projection: ProjectionTy,
|
projection: ProjectionTy,
|
||||||
env: Arc<TraitEnvironment>,
|
env: Arc<TraitEnvironment>,
|
||||||
) -> Ty {
|
) -> Ty {
|
||||||
|
if projection.substitution.iter(Interner).any(|arg| {
|
||||||
|
arg.ty(Interner)
|
||||||
|
.is_some_and(|ty| ty.data(Interner).flags.intersects(TypeFlags::HAS_TY_INFER))
|
||||||
|
}) {
|
||||||
|
never!(
|
||||||
|
"Invoking `normalize_projection_query` with a projection type containing inference var"
|
||||||
|
);
|
||||||
|
return TyKind::Error.intern(Interner);
|
||||||
|
}
|
||||||
|
|
||||||
let mut table = InferenceTable::new(db, env);
|
let mut table = InferenceTable::new(db, env);
|
||||||
let ty = table.normalize_projection_ty(projection);
|
let ty = table.normalize_projection_ty(projection);
|
||||||
table.resolve_completely(ty)
|
table.resolve_completely(ty)
|
||||||
|
|
Loading…
Reference in a new issue