Skip to content

Commit a271dac

Browse files
authored
Merge pull request #20563 from ChayimFriedman2/ns-projection-dyn-auto-trait
fix: When mapping next-solver's `dyn` type, add `Self` (aka. bound var ^1.0) to auto traits' substitutions
2 parents 67a58fd + 867af5f commit a271dac

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

crates/hir-ty/src/mir/lower.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ use super::OperandKind;
5353

5454
mod as_place;
5555
mod pattern_matching;
56+
#[cfg(test)]
57+
mod tests;
5658

5759
#[derive(Debug, Clone)]
5860
struct LoopBlocks {

crates/hir-ty/src/mir/lower/tests.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use hir_def::db::DefDatabase;
2+
use rustc_hash::FxHashMap;
3+
use span::Edition;
4+
use test_fixture::WithFixture;
5+
use triomphe::Arc;
6+
7+
use crate::{
8+
db::HirDatabase,
9+
mir::{MirBody, MirLowerError},
10+
setup_tracing,
11+
test_db::TestDB,
12+
};
13+
14+
fn lower_mir(
15+
#[rust_analyzer::rust_fixture] ra_fixture: &str,
16+
) -> FxHashMap<String, Result<Arc<MirBody>, MirLowerError>> {
17+
let _tracing = setup_tracing();
18+
let (db, file_ids) = TestDB::with_many_files(ra_fixture);
19+
let file_id = *file_ids.last().unwrap();
20+
let module_id = db.module_for_file(file_id.file_id(&db));
21+
let def_map = module_id.def_map(&db);
22+
let scope = &def_map[module_id.local_id].scope;
23+
let funcs = scope.declarations().filter_map(|x| match x {
24+
hir_def::ModuleDefId::FunctionId(it) => Some(it),
25+
_ => None,
26+
});
27+
funcs
28+
.map(|func| {
29+
let name = db.function_signature(func).name.display(&db, Edition::CURRENT).to_string();
30+
let mir = db.mir_body(func.into());
31+
(name, mir)
32+
})
33+
.collect()
34+
}
35+
36+
#[test]
37+
fn dyn_projection_with_auto_traits_regression_next_solver() {
38+
lower_mir(
39+
r#"
40+
//- minicore: sized, send
41+
pub trait Deserializer {}
42+
43+
pub trait Strictest {
44+
type Object: ?Sized;
45+
}
46+
47+
impl Strictest for dyn CustomValue {
48+
type Object = dyn CustomValue + Send;
49+
}
50+
51+
pub trait CustomValue: Send {}
52+
53+
impl CustomValue for () {}
54+
55+
struct Box<T: ?Sized>;
56+
57+
type DeserializeFn<T> = fn(&mut dyn Deserializer) -> Box<T>;
58+
59+
fn foo() {
60+
(|deserializer| Box::new(())) as DeserializeFn<<dyn CustomValue as Strictest>::Object>;
61+
}
62+
"#,
63+
);
64+
}

crates/hir-ty/src/next_solver/mapping.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,10 @@ pub(crate) fn convert_ty_for_result<'db>(interner: DbInterner<'db>, ty: Ty<'db>)
12071207
SolverDefId::TraitId(id) => to_chalk_trait_id(id),
12081208
_ => unreachable!(),
12091209
};
1210-
let substitution = chalk_ir::Substitution::empty(Interner);
1210+
let substitution = chalk_ir::Substitution::from1(
1211+
Interner,
1212+
convert_ty_for_result(interner, self_ty),
1213+
);
12111214
let trait_ref = chalk_ir::TraitRef { trait_id, substitution };
12121215
chalk_ir::WhereClause::Implemented(trait_ref)
12131216
}

0 commit comments

Comments
 (0)