Unnamed repository; edit this file 'description' to name the repository.
Make higher levels adapt Bodys exprs having ExprOrPatId values
Ali Bektas 2025-02-03
parent 06097c3 · commit d8779b4
-rw-r--r--crates/hir/src/diagnostics.rs35
-rw-r--r--crates/hir/src/has_source.rs4
-rw-r--r--crates/hir/src/lib.rs7
-rw-r--r--crates/hir/src/semantics/source_to_def.rs2
-rw-r--r--crates/hir/src/source_analyzer.rs2
-rw-r--r--crates/ide-diagnostics/src/handlers/mismatched_arg_count.rs2
-rw-r--r--crates/ide-diagnostics/src/handlers/unresolved_field.rs7
-rw-r--r--crates/ide-diagnostics/src/handlers/unresolved_method.rs6
8 files changed, 32 insertions, 33 deletions
diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs
index 64e982c42d..9a5e50af07 100644
--- a/crates/hir/src/diagnostics.rs
+++ b/crates/hir/src/diagnostics.rs
@@ -6,6 +6,7 @@
use cfg::{CfgExpr, CfgOptions};
use either::Either;
use hir_def::{
+ body::ExprOrPatPtr,
hir::ExprOrPatId,
path::{hir_segment_to_ast_segment, ModPath},
type_ref::TypesSourceMap,
@@ -115,14 +116,14 @@ diagnostics![
#[derive(Debug)]
pub struct BreakOutsideOfLoop {
- pub expr: InFile<AstPtr<ast::Expr>>,
+ pub expr: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
pub is_break: bool,
pub bad_value_break: bool,
}
#[derive(Debug)]
pub struct TypedHole {
- pub expr: InFile<AstPtr<ast::Expr>>,
+ pub expr: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
pub expected: Type,
}
@@ -234,13 +235,13 @@ pub struct MismatchedTupleStructPatArgCount {
#[derive(Debug)]
pub struct ExpectedFunction {
- pub call: InFile<AstPtr<ast::Expr>>,
+ pub call: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
pub found: Type,
}
#[derive(Debug)]
pub struct UnresolvedField {
- pub expr: InFile<AstPtr<ast::Expr>>,
+ pub expr: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
pub receiver: Type,
pub name: Name,
pub method_with_same_name_exists: bool,
@@ -248,7 +249,7 @@ pub struct UnresolvedField {
#[derive(Debug)]
pub struct UnresolvedMethodCall {
- pub expr: InFile<AstPtr<ast::Expr>>,
+ pub expr: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
pub receiver: Type,
pub name: Name,
pub field_with_same_name: Option<Type>,
@@ -267,7 +268,7 @@ pub struct UnresolvedIdent {
#[derive(Debug)]
pub struct PrivateField {
- pub expr: InFile<AstPtr<ast::Expr>>,
+ pub expr: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
pub field: Field,
}
@@ -302,7 +303,7 @@ pub struct ReplaceFilterMapNextWithFindMap {
#[derive(Debug)]
pub struct MismatchedArgCount {
- pub call_expr: InFile<AstPtr<ast::Expr>>,
+ pub call_expr: InFile<ExprOrPatPtr>,
pub expected: usize,
pub found: usize,
}
@@ -395,13 +396,13 @@ pub struct RemoveUnnecessaryElse {
#[derive(Debug)]
pub struct CastToUnsized {
- pub expr: InFile<AstPtr<ast::Expr>>,
+ pub expr: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
pub cast_ty: Type,
}
#[derive(Debug)]
pub struct InvalidCast {
- pub expr: InFile<AstPtr<ast::Expr>>,
+ pub expr: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
pub error: CastError,
pub expr_ty: Type,
pub cast_ty: Type,
@@ -428,9 +429,7 @@ impl AnyDiagnostic {
.collect();
let record = match record {
- Either::Left(record_expr) => {
- source_map.expr_syntax(record_expr).ok()?.map(AstPtr::wrap_left)
- }
+ Either::Left(record_expr) => source_map.expr_syntax(record_expr).ok()?,
Either::Right(record_pat) => source_map.pat_syntax(record_pat).ok()?,
};
let file = record.file_id;
@@ -474,7 +473,7 @@ impl AnyDiagnostic {
return Some(
ReplaceFilterMapNextWithFindMap {
file: next_source_ptr.file_id,
- next_expr: next_source_ptr.value,
+ next_expr: next_source_ptr.value.cast()?,
}
.into(),
);
@@ -484,7 +483,9 @@ impl AnyDiagnostic {
match source_map.expr_syntax(match_expr) {
Ok(source_ptr) => {
let root = source_ptr.file_syntax(db.upcast());
- if let ast::Expr::MatchExpr(match_expr) = &source_ptr.value.to_node(&root) {
+ if let Either::Left(ast::Expr::MatchExpr(match_expr)) =
+ &source_ptr.value.to_node(&root)
+ {
match match_expr.expr() {
Some(scrut_expr) if match_expr.match_arm_list().is_some() => {
return Some(
@@ -561,7 +562,7 @@ impl AnyDiagnostic {
let pat_syntax =
|pat| source_map.pat_syntax(pat).inspect_err(|_| stdx::never!("synthetic syntax")).ok();
let expr_or_pat_syntax = |id| match id {
- ExprOrPatId::ExprId(expr) => expr_syntax(expr).map(|it| it.map(AstPtr::wrap_left)),
+ ExprOrPatId::ExprId(expr) => expr_syntax(expr),
ExprOrPatId::PatId(pat) => pat_syntax(pat),
};
Some(match d {
@@ -633,7 +634,7 @@ impl AnyDiagnostic {
&InferenceDiagnostic::UnresolvedIdent { id } => {
let node = match id {
ExprOrPatId::ExprId(id) => match source_map.expr_syntax(id) {
- Ok(syntax) => syntax.map(|it| (it.wrap_left(), None)),
+ Ok(syntax) => syntax.map(|it| (it, None)),
Err(SyntheticSyntax) => source_map
.format_args_implicit_capture(id)?
.map(|(node, range)| (node.wrap_left(), Some(range))),
@@ -652,7 +653,7 @@ impl AnyDiagnostic {
}
&InferenceDiagnostic::MismatchedTupleStructPatArgCount { pat, expected, found } => {
let expr_or_pat = match pat {
- ExprOrPatId::ExprId(expr) => expr_syntax(expr)?.map(AstPtr::wrap_left),
+ ExprOrPatId::ExprId(expr) => expr_syntax(expr)?,
ExprOrPatId::PatId(pat) => {
let InFile { file_id, value } = pat_syntax(pat)?;
diff --git a/crates/hir/src/has_source.rs b/crates/hir/src/has_source.rs
index 82c90ac301..a34b498083 100644
--- a/crates/hir/src/has_source.rs
+++ b/crates/hir/src/has_source.rs
@@ -248,7 +248,7 @@ impl HasSource for Param {
let ast @ InFile { file_id, value } = source_map.expr_syntax(expr_id).ok()?;
let root = db.parse_or_expand(file_id);
match value.to_node(&root) {
- ast::Expr::ClosureExpr(it) => it
+ Either::Left(ast::Expr::ClosureExpr(it)) => it
.param_list()?
.params()
.nth(self.idx)
@@ -301,7 +301,7 @@ impl HasSource for InlineAsmOperand {
let root = src.file_syntax(db.upcast());
return src
.map(|ast| match ast.to_node(&root) {
- ast::Expr::AsmExpr(asm) => asm
+ Either::Left(ast::Expr::AsmExpr(asm)) => asm
.asm_pieces()
.filter_map(|it| match it {
ast::AsmPiece::AsmOperandNamed(it) => Some(it),
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 56090bc6b6..26b2819913 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -1957,7 +1957,7 @@ impl DefWithBody {
ExprOrPatId::PatId(pat) => source_map.pat_syntax(pat).map(Either::Right),
};
let expr_or_pat = match expr_or_pat {
- Ok(Either::Left(expr)) => expr.map(AstPtr::wrap_left),
+ Ok(Either::Left(expr)) => expr,
Ok(Either::Right(InFile { file_id, value: pat })) => {
// cast from Either<Pat, SelfParam> -> Either<_, Pat>
let Some(ptr) = AstPtr::try_from_raw(pat.syntax_node_ptr()) else {
@@ -4592,10 +4592,7 @@ impl CaptureUsages {
match span {
mir::MirSpan::ExprId(expr) => {
if let Ok(expr) = source_map.expr_syntax(expr) {
- result.push(CaptureUsageSource {
- is_ref,
- source: expr.map(AstPtr::wrap_left),
- })
+ result.push(CaptureUsageSource { is_ref, source: expr })
}
}
mir::MirSpan::PatId(pat) => {
diff --git a/crates/hir/src/semantics/source_to_def.rs b/crates/hir/src/semantics/source_to_def.rs
index 3c9e7065c4..29d3736bae 100644
--- a/crates/hir/src/semantics/source_to_def.rs
+++ b/crates/hir/src/semantics/source_to_def.rs
@@ -352,7 +352,7 @@ impl SourceToDefCtx<'_, '_> {
let src = src.cloned().map(ast::Pat::from);
let pat_id = source_map.node_pat(src.as_ref())?;
// the pattern could resolve to a constant, verify that this is not the case
- if let crate::Pat::Bind { id, .. } = body[pat_id] {
+ if let crate::Pat::Bind { id, .. } = body[pat_id.as_pat()?] {
Some((container, id))
} else {
None
diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs
index ca239826d4..cf756c6797 100644
--- a/crates/hir/src/source_analyzer.rs
+++ b/crates/hir/src/source_analyzer.rs
@@ -142,7 +142,7 @@ impl SourceAnalyzer {
fn pat_id(&self, pat: &ast::Pat) -> Option<PatId> {
// FIXME: macros, see `expr_id`
let src = InFile { file_id: self.file_id, value: pat };
- self.body_source_map()?.node_pat(src)
+ self.body_source_map()?.node_pat(src).and_then(ExprOrPatId::as_pat)
}
fn binding_id_of_pat(&self, pat: &ast::IdentPat) -> Option<BindingId> {
diff --git a/crates/ide-diagnostics/src/handlers/mismatched_arg_count.rs b/crates/ide-diagnostics/src/handlers/mismatched_arg_count.rs
index 7126617cde..0520bb3fe9 100644
--- a/crates/ide-diagnostics/src/handlers/mismatched_arg_count.rs
+++ b/crates/ide-diagnostics/src/handlers/mismatched_arg_count.rs
@@ -40,7 +40,7 @@ pub(crate) fn mismatched_arg_count(
Diagnostic::new(
DiagnosticCode::RustcHardError("E0107"),
message,
- invalid_args_range(ctx, d.call_expr.map(AstPtr::wrap_left), d.expected, d.found),
+ invalid_args_range(ctx, d.call_expr, d.expected, d.found),
)
}
diff --git a/crates/ide-diagnostics/src/handlers/unresolved_field.rs b/crates/ide-diagnostics/src/handlers/unresolved_field.rs
index 4accd181ca..dfb03eee73 100644
--- a/crates/ide-diagnostics/src/handlers/unresolved_field.rs
+++ b/crates/ide-diagnostics/src/handlers/unresolved_field.rs
@@ -1,5 +1,6 @@
use std::iter;
+use either::Either;
use hir::{db::ExpandDatabase, Adt, FileRange, HasSource, HirDisplay, InFile, Struct, Union};
use ide_db::text_edit::TextEdit;
use ide_db::{
@@ -41,7 +42,7 @@ pub(crate) fn unresolved_field(
),
adjusted_display_range(ctx, d.expr, &|expr| {
Some(
- match expr {
+ match expr.left()? {
ast::Expr::MethodCallExpr(it) => it.name_ref(),
ast::Expr::FieldExpr(it) => it.name_ref(),
_ => None,
@@ -72,7 +73,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::UnresolvedField) -> Option<Vec<A
fn field_fix(ctx: &DiagnosticsContext<'_>, d: &hir::UnresolvedField) -> Option<Assist> {
// Get the FileRange of the invalid field access
let root = ctx.sema.db.parse_or_expand(d.expr.file_id);
- let expr = d.expr.value.to_node(&root);
+ let expr = d.expr.value.to_node(&root).left()?;
let error_range = ctx.sema.original_range_opt(expr.syntax())?;
let field_name = d.name.as_str();
@@ -263,7 +264,7 @@ fn record_field_layout(
// FIXME: We should fill out the call here, move the cursor and trigger signature help
fn method_fix(
ctx: &DiagnosticsContext<'_>,
- expr_ptr: &InFile<AstPtr<ast::Expr>>,
+ expr_ptr: &InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
) -> Option<Assist> {
let root = ctx.sema.db.parse_or_expand(expr_ptr.file_id);
let expr = expr_ptr.value.to_node(&root);
diff --git a/crates/ide-diagnostics/src/handlers/unresolved_method.rs b/crates/ide-diagnostics/src/handlers/unresolved_method.rs
index 4ab649cc16..dd1b593e8f 100644
--- a/crates/ide-diagnostics/src/handlers/unresolved_method.rs
+++ b/crates/ide-diagnostics/src/handlers/unresolved_method.rs
@@ -35,7 +35,7 @@ pub(crate) fn unresolved_method(
),
adjusted_display_range(ctx, d.expr, &|expr| {
Some(
- match expr {
+ match expr.left()? {
ast::Expr::MethodCallExpr(it) => it.name_ref(),
ast::Expr::FieldExpr(it) => it.name_ref(),
_ => None,
@@ -85,7 +85,7 @@ fn field_fix(
let expr_ptr = &d.expr;
let root = ctx.sema.db.parse_or_expand(expr_ptr.file_id);
let expr = expr_ptr.value.to_node(&root);
- let (file_id, range) = match expr {
+ let (file_id, range) = match expr.left()? {
ast::Expr::MethodCallExpr(mcall) => {
let FileRange { range, file_id } =
ctx.sema.original_range_opt(mcall.receiver()?.syntax())?;
@@ -117,7 +117,7 @@ fn assoc_func_fix(ctx: &DiagnosticsContext<'_>, d: &hir::UnresolvedMethodCall) -
let expr_ptr = &d.expr;
let root = db.parse_or_expand(expr_ptr.file_id);
- let expr: ast::Expr = expr_ptr.value.to_node(&root);
+ let expr: ast::Expr = expr_ptr.value.to_node(&root).left()?;
let call = ast::MethodCallExpr::cast(expr.syntax().clone())?;
let range = InFile::new(expr_ptr.file_id, call.syntax().text_range())