Skip to content

Commit

Permalink
RecordInitChecker: remove unnecessary mut
Browse files Browse the repository at this point in the history
  • Loading branch information
sbillig committed Feb 2, 2025
1 parent babd504 commit d018b3b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 21 deletions.
18 changes: 8 additions & 10 deletions crates/hir-analysis/src/ty/ty_check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use super::{
};
use crate::{
name_resolution::{
diagnostics::NameResDiag, is_scope_visible_from, resolve_name_res, resolve_path,
resolve_query, EarlyNameQueryId, NameDomain, NameResBucket, PathRes, QueryDirective,
diagnostics::NameResDiag, is_scope_visible_from, resolve_name_res, resolve_query,
EarlyNameQueryId, NameDomain, NameResBucket, PathRes, QueryDirective,
},
ty::{
canonical::Canonicalized,
Expand Down Expand Up @@ -507,14 +507,13 @@ impl<'db> TyChecker<'db> {
return ExprProp::invalid(self.db);
};

let Ok(reso) = resolve_path(self.db, *path, self.env.scope(), true) else {
let Ok(reso) = self.resolve_path(*path, true) else {
return ExprProp::invalid(self.db);
};

match reso {
PathRes::Ty(ty) if ty.is_record(self.db) => {
let ty = self.table.instantiate_to_term(ty);
self.check_record_init_fields(ty, expr);
self.check_record_init_fields(&ty, expr);
ExprProp::new(ty, true)
}

Expand All @@ -531,9 +530,8 @@ impl<'db> TyChecker<'db> {

PathRes::EnumVariant(variant) => {
if variant.is_record(self.db) {
let ty = self.table.instantiate_to_term(variant.ty);
self.check_record_init_fields(variant, expr);
ExprProp::new(ty, true)
self.check_record_init_fields(&variant, expr);
ExprProp::new(variant.ty, true)
} else {
let diag =
BodyDiag::record_expected::<TyId<'db>>(self.db, span.path().into(), None);
Expand Down Expand Up @@ -561,15 +559,15 @@ impl<'db> TyChecker<'db> {
}
}

fn check_record_init_fields<T: RecordLike<'db>>(&mut self, mut record_like: T, expr: ExprId) {
fn check_record_init_fields<T: RecordLike<'db>>(&mut self, record_like: &T, expr: ExprId) {
let hir_db = self.db.as_hir_db();

let Partial::Present(Expr::RecordInit(_, fields)) = expr.data(hir_db, self.body()) else {
unreachable!()
};
let span = expr.lazy_span(self.body()).into_record_init_expr();

let mut rec_checker = RecordInitChecker::new(self, &mut record_like);
let mut rec_checker = RecordInitChecker::new(self, record_like);

for (i, field) in fields.iter().enumerate() {
let label = field.label_eagerly(rec_checker.tc.db.as_hir_db(), rec_checker.tc.body());
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-analysis/src/ty/ty_check/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ impl<'db> TyChecker<'db> {
}
}

fn check_record_pat_fields<T>(&mut self, mut record_like: T, pat: PatId)
fn check_record_pat_fields<T>(&mut self, record_like: T, pat: PatId)
where
T: RecordLike<'db>,
{
Expand All @@ -394,7 +394,7 @@ impl<'db> TyChecker<'db> {
let mut contains_rest = false;

let pat_span = pat.lazy_span(self.body()).into_record_pat();
let mut rec_checker = RecordInitChecker::new(self, &mut record_like);
let mut rec_checker = RecordInitChecker::new(self, &record_like);

for (i, field_pat) in fields.iter().enumerate() {
let field_pat_span = pat_span.fields().field(i);
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-analysis/src/ty/ty_check/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub(super) enum ResolvedPathInBody<'db> {

pub(super) struct RecordInitChecker<'tc, 'db, 'a, T> {
pub(super) tc: &'tc mut TyChecker<'db>,
data: &'a mut T,
data: &'a T,
already_given: FxHashMap<IdentId<'db>, DynLazySpan<'db>>,
invalid_field_given: bool,
}
Expand All @@ -60,7 +60,7 @@ where
///
/// ## Panics
/// Panics if the given `data` is not a record.
pub(super) fn new(tc: &'tc mut TyChecker<'db>, data: &'a mut T) -> Self {
pub(super) fn new(tc: &'tc mut TyChecker<'db>, data: &'a T) -> Self {
assert!(data.is_record(tc.db));

Self {
Expand Down
16 changes: 12 additions & 4 deletions crates/hir-analysis/test_files/ty_check/record_init.fe
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ struct Foo {
y: String<10>
}

fn foo() {
fn foo() {
let x = 1
let y = "FOO"

let f = Foo {x, y}

let f2 = Foo {x: 1, y: "FOO"}

let f3 = Foo {y: "FOO", x: 1}
Expand All @@ -23,7 +23,7 @@ fn foo2<Z>(b: bool, z: Z) {
let t = false
let u = "Bar"
let f = Bar {t, u}

let f2 = Bar {t: z, u: f}
}

Expand All @@ -36,4 +36,12 @@ where T: * -> * -> *
fn foo3() {
let bar = Bar { t: 1, u: false }
let x = Wrapper { t: bar }
}
}

enum G<T> {
V1 { val: T }
}

fn generic_path() {
let g = G<u32>::V1 { val: 10 }
}
33 changes: 30 additions & 3 deletions crates/hir-analysis/test_files/ty_check/record_init.snap
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
source: crates/hir-analysis/tests/ty_check.rs
expression: res
input_file: crates/hir-analysis/test_files/ty_check/record_init.fe
input_file: test_files/ty_check/record_init.fe
---
note:
┌─ record_init.fe:6:10
6fn foo() {
6fn foo() {
│ ╭──────────^
7 │ │ let x = 1
8 │ │ let y = "FOO"
Expand Down Expand Up @@ -120,7 +120,7 @@ note:
23 │ │ let t = false
24 │ │ let u = "Bar"
25 │ │ let f = Bar {t, u}
26 │ │
26 │ │
27 │ │ let f2 = Bar {t: z, u: f}
28 │ │ }
│ ╰─^ ()
Expand Down Expand Up @@ -248,3 +248,30 @@ note:
38let x = Wrapper { t: bar }
^^^ Bar<i32, bool>

note:
┌─ record_init.fe:45:19
45fn generic_path() {
│ ╭───────────────────^
46 │ │ let g = G<u32>::V1 { val: 10 }
47 │ │ }
│ ╰─^ ()

note:
┌─ record_init.fe:46:9
46 │ let g = G<u32>::V1 { val: 10 }
│ ^ G<u32>

note:
┌─ record_init.fe:46:13
46 │ let g = G<u32>::V1 { val: 10 }
│ ^^^^^^^^^^^^^^^^^^^^^^ G<u32>

note:
┌─ record_init.fe:46:31
46 │ let g = G<u32>::V1 { val: 10 }
│ ^^ u32

0 comments on commit d018b3b

Please # to comment.