Skip to content

Commit d824511

Browse files
committed
Auto merge of rust-lang#15297 - HKalbasi:mir, r=HKalbasi
Normalize type alias in projected_ty fix rust-lang#15143
2 parents ca797d2 + 832eb0d commit d824511

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

crates/hir-ty/src/mir.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
use std::{fmt::Display, iter};
44

55
use crate::{
6-
consteval::usize_const, db::HirDatabase, display::HirDisplay, infer::PointerCast,
7-
lang_items::is_box, mapping::ToChalk, CallableDefId, ClosureId, Const, ConstScalar,
8-
InferenceResult, Interner, MemoryMap, Substitution, Ty, TyKind,
6+
consteval::usize_const,
7+
db::HirDatabase,
8+
display::HirDisplay,
9+
infer::{normalize, PointerCast},
10+
lang_items::is_box,
11+
mapping::ToChalk,
12+
CallableDefId, ClosureId, Const, ConstScalar, InferenceResult, Interner, MemoryMap,
13+
Substitution, TraitEnvironment, Ty, TyKind,
914
};
1015
use base_db::CrateId;
1116
use chalk_ir::Mutability;
@@ -34,6 +39,7 @@ pub use monomorphization::{
3439
};
3540
use smallvec::{smallvec, SmallVec};
3641
use stdx::{impl_from, never};
42+
use triomphe::Arc;
3743

3844
use super::consteval::{intern_const_scalar, try_const_usize};
3945

@@ -131,11 +137,19 @@ pub enum ProjectionElem<V, T> {
131137
impl<V, T> ProjectionElem<V, T> {
132138
pub fn projected_ty(
133139
&self,
134-
base: Ty,
140+
mut base: Ty,
135141
db: &dyn HirDatabase,
136142
closure_field: impl FnOnce(ClosureId, &Substitution, usize) -> Ty,
137143
krate: CrateId,
138144
) -> Ty {
145+
if matches!(base.data(Interner).kind, TyKind::Alias(_) | TyKind::AssociatedType(..)) {
146+
base = normalize(
147+
db,
148+
// FIXME: we should get this from caller
149+
Arc::new(TraitEnvironment::empty(krate)),
150+
base,
151+
);
152+
}
139153
match self {
140154
ProjectionElem::Deref => match &base.data(Interner).kind {
141155
TyKind::Raw(_, inner) | TyKind::Ref(_, _, inner) => inner.clone(),

crates/ide-diagnostics/src/handlers/mutability_errors.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,33 @@ fn f() {
10841084
);
10851085
}
10861086

1087+
#[test]
1088+
fn regression_15143() {
1089+
check_diagnostics(
1090+
r#"
1091+
trait Tr {
1092+
type Ty;
1093+
}
1094+
1095+
struct A;
1096+
1097+
impl Tr for A {
1098+
type Ty = (u32, i64);
1099+
}
1100+
1101+
struct B<T: Tr> {
1102+
f: <T as Tr>::Ty,
1103+
}
1104+
1105+
fn main(b: B<A>) {
1106+
let f = b.f.0;
1107+
f = 5;
1108+
//^^^^^ 💡 error: cannot mutate immutable variable `f`
1109+
}
1110+
"#,
1111+
);
1112+
}
1113+
10871114
#[test]
10881115
fn allow_unused_mut_for_identifiers_starting_with_underline() {
10891116
check_diagnostics(

0 commit comments

Comments
 (0)