Skip to content

Commit e1ff9a0

Browse files
committed
v2
1 parent 3942bab commit e1ff9a0

File tree

4 files changed

+49
-48
lines changed

4 files changed

+49
-48
lines changed

crates/hir-def/src/expr_store/lower.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,23 +1798,17 @@ impl ExprCollector<'_> {
17981798
Expr::Literal(pat_literal_to_hir(it)?.0),
17991799
ptr,
18001800
)),
1801-
ast::Pat::IdentPat(ident) => {
1802-
if ident.is_simple_ident() {
1803-
return ident
1804-
.name()
1805-
.map(|name| name.as_name())
1806-
.map(Path::from)
1807-
.map(|path| {
1808-
self.alloc_expr_from_pat(Expr::Path(path), ptr)
1809-
});
1810-
}
1811-
1812-
None
1813-
}
1801+
ast::Pat::IdentPat(ident) if ident.is_simple_ident() => ident
1802+
.name()
1803+
.map(|name| name.as_name())
1804+
.map(Path::from)
1805+
.map(|path| self.alloc_expr_from_pat(Expr::Path(path), ptr)),
18141806
ast::Pat::PathPat(p) => p
18151807
.path()
18161808
.and_then(|path| self.parse_path(path))
18171809
.map(|parsed| self.alloc_expr_from_pat(Expr::Path(parsed), ptr)),
1810+
// We only need to handle literal, ident (if bare) and path patterns here,
1811+
// as any other pattern as a range pattern operand is semantically invalid.
18181812
_ => None,
18191813
}
18201814
})
@@ -2545,7 +2539,7 @@ impl ExprCollector<'_> {
25452539

25462540
fn alloc_expr_from_pat(&mut self, expr: Expr, ptr: PatPtr) -> ExprId {
25472541
let src = self.expander.in_file(ptr);
2548-
let id = self.body.exprs.alloc(expr);
2542+
let id = self.store.exprs.alloc(expr);
25492543
self.source_map.pat_map.insert(src, id.into());
25502544
self.source_map.expr_map_back.insert(id, src.map(AstPtr::wrap_right));
25512545
id

crates/hir/src/diagnostics.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use cfg::{CfgExpr, CfgOptions};
77
use either::Either;
88
use hir_def::{
9-
body::ExprOrPatPtr,
9+
expr_store::ExprOrPatPtr,
1010
hir::ExprOrPatId,
1111
path::{hir_segment_to_ast_segment, ModPath},
1212
type_ref::TypesSourceMap,
@@ -116,14 +116,14 @@ diagnostics![
116116

117117
#[derive(Debug)]
118118
pub struct BreakOutsideOfLoop {
119-
pub expr: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
119+
pub expr: InFile<ExprOrPatPtr>,
120120
pub is_break: bool,
121121
pub bad_value_break: bool,
122122
}
123123

124124
#[derive(Debug)]
125125
pub struct TypedHole {
126-
pub expr: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
126+
pub expr: InFile<ExprOrPatPtr>,
127127
pub expected: Type,
128128
}
129129

@@ -222,34 +222,34 @@ pub struct NoSuchField {
222222

223223
#[derive(Debug)]
224224
pub struct PrivateAssocItem {
225-
pub expr_or_pat: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
225+
pub expr_or_pat: InFile<ExprOrPatPtr>,
226226
pub item: AssocItem,
227227
}
228228

229229
#[derive(Debug)]
230230
pub struct MismatchedTupleStructPatArgCount {
231-
pub expr_or_pat: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
231+
pub expr_or_pat: InFile<ExprOrPatPtr>,
232232
pub expected: usize,
233233
pub found: usize,
234234
}
235235

236236
#[derive(Debug)]
237237
pub struct ExpectedFunction {
238-
pub call: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
238+
pub call: InFile<ExprOrPatPtr>,
239239
pub found: Type,
240240
}
241241

242242
#[derive(Debug)]
243243
pub struct UnresolvedField {
244-
pub expr: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
244+
pub expr: InFile<ExprOrPatPtr>,
245245
pub receiver: Type,
246246
pub name: Name,
247247
pub method_with_same_name_exists: bool,
248248
}
249249

250250
#[derive(Debug)]
251251
pub struct UnresolvedMethodCall {
252-
pub expr: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
252+
pub expr: InFile<ExprOrPatPtr>,
253253
pub receiver: Type,
254254
pub name: Name,
255255
pub field_with_same_name: Option<Type>,
@@ -258,17 +258,17 @@ pub struct UnresolvedMethodCall {
258258

259259
#[derive(Debug)]
260260
pub struct UnresolvedAssocItem {
261-
pub expr_or_pat: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
261+
pub expr_or_pat: InFile<ExprOrPatPtr>,
262262
}
263263

264264
#[derive(Debug)]
265265
pub struct UnresolvedIdent {
266-
pub node: InFile<(AstPtr<Either<ast::Expr, ast::Pat>>, Option<TextRange>)>,
266+
pub node: InFile<(ExprOrPatPtr, Option<TextRange>)>,
267267
}
268268

269269
#[derive(Debug)]
270270
pub struct PrivateField {
271-
pub expr: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
271+
pub expr: InFile<ExprOrPatPtr>,
272272
pub field: Field,
273273
}
274274

@@ -281,7 +281,7 @@ pub enum UnsafeLint {
281281

282282
#[derive(Debug)]
283283
pub struct MissingUnsafe {
284-
pub node: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
284+
pub node: InFile<ExprOrPatPtr>,
285285
pub lint: UnsafeLint,
286286
pub reason: UnsafetyReason,
287287
}
@@ -322,7 +322,7 @@ pub struct NonExhaustiveLet {
322322

323323
#[derive(Debug)]
324324
pub struct TypeMismatch {
325-
pub expr_or_pat: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
325+
pub expr_or_pat: InFile<ExprOrPatPtr>,
326326
pub expected: Type,
327327
pub actual: Type,
328328
}
@@ -396,13 +396,13 @@ pub struct RemoveUnnecessaryElse {
396396

397397
#[derive(Debug)]
398398
pub struct CastToUnsized {
399-
pub expr: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
399+
pub expr: InFile<ExprOrPatPtr>,
400400
pub cast_ty: Type,
401401
}
402402

403403
#[derive(Debug)]
404404
pub struct InvalidCast {
405-
pub expr: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
405+
pub expr: InFile<ExprOrPatPtr>,
406406
pub error: CastError,
407407
pub expr_ty: Type,
408408
pub cast_ty: Type,

crates/hir/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2003,7 +2003,7 @@ impl DefWithBody {
20032003
match source_map.expr_syntax(node) {
20042004
Ok(node) => acc.push(
20052005
MissingUnsafe {
2006-
node: node.map(|it| it.wrap_left()),
2006+
node,
20072007
lint: UnsafeLint::DeprecatedSafe2024,
20082008
reason: UnsafetyReason::UnsafeFnCall,
20092009
}

crates/hir/src/source_analyzer.rs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,20 @@ impl SourceAnalyzer {
210210
db: &dyn HirDatabase,
211211
pat: &ast::Pat,
212212
) -> Option<(Type, Option<Type>)> {
213-
let pat_id = self.pat_id(pat)?;
213+
let expr_or_pat_id = self.pat_id(pat)?;
214214
let infer = self.infer.as_ref()?;
215-
let coerced = infer
216-
.pat_adjustments
217-
.get(&pat_id.as_pat()?)
218-
.and_then(|adjusts| adjusts.last().cloned());
219-
let ty = infer[pat_id].clone();
215+
let coerced = match expr_or_pat_id {
216+
ExprOrPatId::ExprId(idx) => infer
217+
.expr_adjustments
218+
.get(&idx)
219+
.and_then(|adjusts| adjusts.last().cloned())
220+
.map(|adjust| adjust.target),
221+
ExprOrPatId::PatId(idx) => {
222+
infer.pat_adjustments.get(&idx).and_then(|adjusts| adjusts.last().cloned())
223+
}
224+
};
225+
226+
let ty = infer[expr_or_pat_id].clone();
220227
let mk_ty = |ty| Type::new_with_resolver(db, &self.resolver, ty);
221228
Some((mk_ty(ty), coerced.map(mk_ty)))
222229
}
@@ -684,19 +691,18 @@ impl SourceAnalyzer {
684691
db: &dyn HirDatabase,
685692
pat: &ast::IdentPat,
686693
) -> Option<ModuleDef> {
687-
let pat_id = self.pat_id(&pat.clone().into())?;
694+
let expr_or_pat_id = self.pat_id(&pat.clone().into())?;
688695
let body = self.body()?;
689696

690-
let path = if pat_id.is_pat() {
691-
match &body[pat_id.as_pat()?] {
692-
Pat::Path(path) => path,
693-
_ => return None,
694-
}
695-
} else {
696-
match &body[pat_id.as_expr()?] {
697+
let path = match expr_or_pat_id {
698+
ExprOrPatId::ExprId(idx) => match &body[idx] {
697699
Expr::Path(path) => path,
698700
_ => return None,
699-
}
701+
},
702+
ExprOrPatId::PatId(idx) => match &body[idx] {
703+
Pat::Path(path) => path,
704+
_ => return None,
705+
},
700706
};
701707

702708
let res = resolve_hir_path(db, &self.resolver, path, HygieneId::ROOT, TypesMap::EMPTY)?;
@@ -793,8 +799,9 @@ impl SourceAnalyzer {
793799
}
794800
prefer_value_ns = true;
795801
} else if let Some(path_pat) = parent().and_then(ast::PathPat::cast) {
796-
let pat_id = self.pat_id(&path_pat.into())?;
797-
if let Some((assoc, subs)) = infer.assoc_resolutions_for_pat(pat_id.as_pat()?) {
802+
let expr_or_pat_id = self.pat_id(&path_pat.into())?;
803+
if let Some((assoc, subs)) = infer.assoc_resolutions_for_expr_or_pat(expr_or_pat_id)
804+
{
798805
let (assoc, subst) = match assoc {
799806
AssocItemId::ConstId(const_id) => {
800807
let (konst, subst) =
@@ -818,7 +825,7 @@ impl SourceAnalyzer {
818825
return Some((PathResolution::Def(AssocItem::from(assoc).into()), Some(subst)));
819826
}
820827
if let Some(VariantId::EnumVariantId(variant)) =
821-
infer.variant_resolution_for_pat(pat_id.as_pat()?)
828+
infer.variant_resolution_for_expr_or_pat(expr_or_pat_id)
822829
{
823830
return Some((PathResolution::Def(ModuleDef::Variant(variant.into())), None));
824831
}

0 commit comments

Comments
 (0)