Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 31a5685

Browse files
committed
Make higher levels adapt Bodys exprs having ExprOrPatId values
1 parent ee6b5cf commit 31a5685

File tree

8 files changed

+32
-33
lines changed

8 files changed

+32
-33
lines changed

src/tools/rust-analyzer/crates/hir/src/diagnostics.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use cfg::{CfgExpr, CfgOptions};
77
use either::Either;
88
use hir_def::{
9+
body::ExprOrPatPtr,
910
hir::ExprOrPatId,
1011
path::{hir_segment_to_ast_segment, ModPath},
1112
type_ref::TypesSourceMap,
@@ -115,14 +116,14 @@ diagnostics![
115116

116117
#[derive(Debug)]
117118
pub struct BreakOutsideOfLoop {
118-
pub expr: InFile<AstPtr<ast::Expr>>,
119+
pub expr: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
119120
pub is_break: bool,
120121
pub bad_value_break: bool,
121122
}
122123

123124
#[derive(Debug)]
124125
pub struct TypedHole {
125-
pub expr: InFile<AstPtr<ast::Expr>>,
126+
pub expr: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
126127
pub expected: Type,
127128
}
128129

@@ -234,21 +235,21 @@ pub struct MismatchedTupleStructPatArgCount {
234235

235236
#[derive(Debug)]
236237
pub struct ExpectedFunction {
237-
pub call: InFile<AstPtr<ast::Expr>>,
238+
pub call: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
238239
pub found: Type,
239240
}
240241

241242
#[derive(Debug)]
242243
pub struct UnresolvedField {
243-
pub expr: InFile<AstPtr<ast::Expr>>,
244+
pub expr: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
244245
pub receiver: Type,
245246
pub name: Name,
246247
pub method_with_same_name_exists: bool,
247248
}
248249

249250
#[derive(Debug)]
250251
pub struct UnresolvedMethodCall {
251-
pub expr: InFile<AstPtr<ast::Expr>>,
252+
pub expr: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
252253
pub receiver: Type,
253254
pub name: Name,
254255
pub field_with_same_name: Option<Type>,
@@ -267,7 +268,7 @@ pub struct UnresolvedIdent {
267268

268269
#[derive(Debug)]
269270
pub struct PrivateField {
270-
pub expr: InFile<AstPtr<ast::Expr>>,
271+
pub expr: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
271272
pub field: Field,
272273
}
273274

@@ -302,7 +303,7 @@ pub struct ReplaceFilterMapNextWithFindMap {
302303

303304
#[derive(Debug)]
304305
pub struct MismatchedArgCount {
305-
pub call_expr: InFile<AstPtr<ast::Expr>>,
306+
pub call_expr: InFile<ExprOrPatPtr>,
306307
pub expected: usize,
307308
pub found: usize,
308309
}
@@ -395,13 +396,13 @@ pub struct RemoveUnnecessaryElse {
395396

396397
#[derive(Debug)]
397398
pub struct CastToUnsized {
398-
pub expr: InFile<AstPtr<ast::Expr>>,
399+
pub expr: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
399400
pub cast_ty: Type,
400401
}
401402

402403
#[derive(Debug)]
403404
pub struct InvalidCast {
404-
pub expr: InFile<AstPtr<ast::Expr>>,
405+
pub expr: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
405406
pub error: CastError,
406407
pub expr_ty: Type,
407408
pub cast_ty: Type,
@@ -428,9 +429,7 @@ impl AnyDiagnostic {
428429
.collect();
429430

430431
let record = match record {
431-
Either::Left(record_expr) => {
432-
source_map.expr_syntax(record_expr).ok()?.map(AstPtr::wrap_left)
433-
}
432+
Either::Left(record_expr) => source_map.expr_syntax(record_expr).ok()?,
434433
Either::Right(record_pat) => source_map.pat_syntax(record_pat).ok()?,
435434
};
436435
let file = record.file_id;
@@ -474,7 +473,7 @@ impl AnyDiagnostic {
474473
return Some(
475474
ReplaceFilterMapNextWithFindMap {
476475
file: next_source_ptr.file_id,
477-
next_expr: next_source_ptr.value,
476+
next_expr: next_source_ptr.value.cast()?,
478477
}
479478
.into(),
480479
);
@@ -484,7 +483,9 @@ impl AnyDiagnostic {
484483
match source_map.expr_syntax(match_expr) {
485484
Ok(source_ptr) => {
486485
let root = source_ptr.file_syntax(db.upcast());
487-
if let ast::Expr::MatchExpr(match_expr) = &source_ptr.value.to_node(&root) {
486+
if let Either::Left(ast::Expr::MatchExpr(match_expr)) =
487+
&source_ptr.value.to_node(&root)
488+
{
488489
match match_expr.expr() {
489490
Some(scrut_expr) if match_expr.match_arm_list().is_some() => {
490491
return Some(
@@ -561,7 +562,7 @@ impl AnyDiagnostic {
561562
let pat_syntax =
562563
|pat| source_map.pat_syntax(pat).inspect_err(|_| stdx::never!("synthetic syntax")).ok();
563564
let expr_or_pat_syntax = |id| match id {
564-
ExprOrPatId::ExprId(expr) => expr_syntax(expr).map(|it| it.map(AstPtr::wrap_left)),
565+
ExprOrPatId::ExprId(expr) => expr_syntax(expr),
565566
ExprOrPatId::PatId(pat) => pat_syntax(pat),
566567
};
567568
Some(match d {
@@ -633,7 +634,7 @@ impl AnyDiagnostic {
633634
&InferenceDiagnostic::UnresolvedIdent { id } => {
634635
let node = match id {
635636
ExprOrPatId::ExprId(id) => match source_map.expr_syntax(id) {
636-
Ok(syntax) => syntax.map(|it| (it.wrap_left(), None)),
637+
Ok(syntax) => syntax.map(|it| (it, None)),
637638
Err(SyntheticSyntax) => source_map
638639
.format_args_implicit_capture(id)?
639640
.map(|(node, range)| (node.wrap_left(), Some(range))),
@@ -652,7 +653,7 @@ impl AnyDiagnostic {
652653
}
653654
&InferenceDiagnostic::MismatchedTupleStructPatArgCount { pat, expected, found } => {
654655
let expr_or_pat = match pat {
655-
ExprOrPatId::ExprId(expr) => expr_syntax(expr)?.map(AstPtr::wrap_left),
656+
ExprOrPatId::ExprId(expr) => expr_syntax(expr)?,
656657
ExprOrPatId::PatId(pat) => {
657658
let InFile { file_id, value } = pat_syntax(pat)?;
658659

src/tools/rust-analyzer/crates/hir/src/has_source.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ impl HasSource for Param {
248248
let ast @ InFile { file_id, value } = source_map.expr_syntax(expr_id).ok()?;
249249
let root = db.parse_or_expand(file_id);
250250
match value.to_node(&root) {
251-
ast::Expr::ClosureExpr(it) => it
251+
Either::Left(ast::Expr::ClosureExpr(it)) => it
252252
.param_list()?
253253
.params()
254254
.nth(self.idx)
@@ -301,7 +301,7 @@ impl HasSource for InlineAsmOperand {
301301
let root = src.file_syntax(db.upcast());
302302
return src
303303
.map(|ast| match ast.to_node(&root) {
304-
ast::Expr::AsmExpr(asm) => asm
304+
Either::Left(ast::Expr::AsmExpr(asm)) => asm
305305
.asm_pieces()
306306
.filter_map(|it| match it {
307307
ast::AsmPiece::AsmOperandNamed(it) => Some(it),

src/tools/rust-analyzer/crates/hir/src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,7 +1957,7 @@ impl DefWithBody {
19571957
ExprOrPatId::PatId(pat) => source_map.pat_syntax(pat).map(Either::Right),
19581958
};
19591959
let expr_or_pat = match expr_or_pat {
1960-
Ok(Either::Left(expr)) => expr.map(AstPtr::wrap_left),
1960+
Ok(Either::Left(expr)) => expr,
19611961
Ok(Either::Right(InFile { file_id, value: pat })) => {
19621962
// cast from Either<Pat, SelfParam> -> Either<_, Pat>
19631963
let Some(ptr) = AstPtr::try_from_raw(pat.syntax_node_ptr()) else {
@@ -4592,10 +4592,7 @@ impl CaptureUsages {
45924592
match span {
45934593
mir::MirSpan::ExprId(expr) => {
45944594
if let Ok(expr) = source_map.expr_syntax(expr) {
4595-
result.push(CaptureUsageSource {
4596-
is_ref,
4597-
source: expr.map(AstPtr::wrap_left),
4598-
})
4595+
result.push(CaptureUsageSource { is_ref, source: expr })
45994596
}
46004597
}
46014598
mir::MirSpan::PatId(pat) => {

src/tools/rust-analyzer/crates/hir/src/semantics/source_to_def.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ impl SourceToDefCtx<'_, '_> {
352352
let src = src.cloned().map(ast::Pat::from);
353353
let pat_id = source_map.node_pat(src.as_ref())?;
354354
// the pattern could resolve to a constant, verify that this is not the case
355-
if let crate::Pat::Bind { id, .. } = body[pat_id] {
355+
if let crate::Pat::Bind { id, .. } = body[pat_id.as_pat()?] {
356356
Some((container, id))
357357
} else {
358358
None

src/tools/rust-analyzer/crates/hir/src/source_analyzer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl SourceAnalyzer {
142142
fn pat_id(&self, pat: &ast::Pat) -> Option<PatId> {
143143
// FIXME: macros, see `expr_id`
144144
let src = InFile { file_id: self.file_id, value: pat };
145-
self.body_source_map()?.node_pat(src)
145+
self.body_source_map()?.node_pat(src).and_then(ExprOrPatId::as_pat)
146146
}
147147

148148
fn binding_id_of_pat(&self, pat: &ast::IdentPat) -> Option<BindingId> {

src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/mismatched_arg_count.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub(crate) fn mismatched_arg_count(
4040
Diagnostic::new(
4141
DiagnosticCode::RustcHardError("E0107"),
4242
message,
43-
invalid_args_range(ctx, d.call_expr.map(AstPtr::wrap_left), d.expected, d.found),
43+
invalid_args_range(ctx, d.call_expr, d.expected, d.found),
4444
)
4545
}
4646

src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/unresolved_field.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::iter;
22

3+
use either::Either;
34
use hir::{db::ExpandDatabase, Adt, FileRange, HasSource, HirDisplay, InFile, Struct, Union};
45
use ide_db::text_edit::TextEdit;
56
use ide_db::{
@@ -41,7 +42,7 @@ pub(crate) fn unresolved_field(
4142
),
4243
adjusted_display_range(ctx, d.expr, &|expr| {
4344
Some(
44-
match expr {
45+
match expr.left()? {
4546
ast::Expr::MethodCallExpr(it) => it.name_ref(),
4647
ast::Expr::FieldExpr(it) => it.name_ref(),
4748
_ => None,
@@ -72,7 +73,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::UnresolvedField) -> Option<Vec<A
7273
fn field_fix(ctx: &DiagnosticsContext<'_>, d: &hir::UnresolvedField) -> Option<Assist> {
7374
// Get the FileRange of the invalid field access
7475
let root = ctx.sema.db.parse_or_expand(d.expr.file_id);
75-
let expr = d.expr.value.to_node(&root);
76+
let expr = d.expr.value.to_node(&root).left()?;
7677

7778
let error_range = ctx.sema.original_range_opt(expr.syntax())?;
7879
let field_name = d.name.as_str();
@@ -263,7 +264,7 @@ fn record_field_layout(
263264
// FIXME: We should fill out the call here, move the cursor and trigger signature help
264265
fn method_fix(
265266
ctx: &DiagnosticsContext<'_>,
266-
expr_ptr: &InFile<AstPtr<ast::Expr>>,
267+
expr_ptr: &InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
267268
) -> Option<Assist> {
268269
let root = ctx.sema.db.parse_or_expand(expr_ptr.file_id);
269270
let expr = expr_ptr.value.to_node(&root);

src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/unresolved_method.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub(crate) fn unresolved_method(
3535
),
3636
adjusted_display_range(ctx, d.expr, &|expr| {
3737
Some(
38-
match expr {
38+
match expr.left()? {
3939
ast::Expr::MethodCallExpr(it) => it.name_ref(),
4040
ast::Expr::FieldExpr(it) => it.name_ref(),
4141
_ => None,
@@ -85,7 +85,7 @@ fn field_fix(
8585
let expr_ptr = &d.expr;
8686
let root = ctx.sema.db.parse_or_expand(expr_ptr.file_id);
8787
let expr = expr_ptr.value.to_node(&root);
88-
let (file_id, range) = match expr {
88+
let (file_id, range) = match expr.left()? {
8989
ast::Expr::MethodCallExpr(mcall) => {
9090
let FileRange { range, file_id } =
9191
ctx.sema.original_range_opt(mcall.receiver()?.syntax())?;
@@ -117,7 +117,7 @@ fn assoc_func_fix(ctx: &DiagnosticsContext<'_>, d: &hir::UnresolvedMethodCall) -
117117

118118
let expr_ptr = &d.expr;
119119
let root = db.parse_or_expand(expr_ptr.file_id);
120-
let expr: ast::Expr = expr_ptr.value.to_node(&root);
120+
let expr: ast::Expr = expr_ptr.value.to_node(&root).left()?;
121121

122122
let call = ast::MethodCallExpr::cast(expr.syntax().clone())?;
123123
let range = InFile::new(expr_ptr.file_id, call.syntax().text_range())

0 commit comments

Comments
 (0)