Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/hir-def/src/expr_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ pub enum ExpressionStoreDiagnostics {
UnreachableLabel { node: InFile<AstPtr<ast::Lifetime>>, name: Name },
AwaitOutsideOfAsync { node: InFile<AstPtr<ast::AwaitExpr>>, location: String },
UndeclaredLabel { node: InFile<AstPtr<ast::Lifetime>>, name: Name },
BaseExprInStructPattern { node: InFile<AstPtr<ast::Expr>> },
}

impl ExpressionStoreBuilder {
Expand Down
10 changes: 8 additions & 2 deletions crates/hir-def/src/expr_store/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1806,7 +1806,13 @@ impl<'db> ExprCollector<'db> {
};
let record_field_list = e.record_expr_field_list()?;
let ellipsis = record_field_list.dotdot_token().is_some();
// FIXME: Report an error here if `record_field_list.spread().is_some()`.
if let Some(spread) = record_field_list.spread() {
self.store.diagnostics.push(
ExpressionStoreDiagnostics::BaseExprInStructPattern {
node: self.expander.in_file(AstPtr::new(&spread)),
},
);
}
let args = record_field_list
.fields()
.filter_map(|f| {
Expand Down Expand Up @@ -2817,7 +2823,7 @@ impl<'db> ExprCollector<'db> {

// endregion: patterns

/// Returns `None` (and emits diagnostics) when `owner` if `#[cfg]`d out, and `Some(())` when
/// Returns `false` (and emits diagnostics) when `owner` if `#[cfg]`d out, and `true` when
/// not.
fn check_cfg(&mut self, owner: &dyn ast::HasAttrs) -> bool {
let enabled = self.expander.is_cfg_enabled(owner, self.cfg_options);
Expand Down
6 changes: 6 additions & 0 deletions crates/hir/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ macro_rules! diagnostics {

diagnostics![AnyDiagnostic<'db> ->
AwaitOutsideOfAsync,
BaseExprInStructPattern,
BreakOutsideOfLoop,
CastToUnsized<'db>,
ExpectedFunction<'db>,
Expand Down Expand Up @@ -486,6 +487,11 @@ pub struct InvalidLhsOfAssignment {
pub lhs: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
}

#[derive(Debug)]
pub struct BaseExprInStructPattern {
pub node: InFile<AstPtr<ast::Expr>>,
}

impl<'db> AnyDiagnostic<'db> {
pub(crate) fn body_validation_diagnostic(
db: &'db dyn HirDatabase,
Expand Down
3 changes: 3 additions & 0 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2388,6 +2388,9 @@ fn expr_store_diagnostics<'db>(
ExpressionStoreDiagnostics::UndeclaredLabel { node, name } => {
UndeclaredLabel { node: *node, name: name.clone() }.into()
}
ExpressionStoreDiagnostics::BaseExprInStructPattern { node } => {
BaseExprInStructPattern { node: *node }.into()
}
});
}

Expand Down
65 changes: 65 additions & 0 deletions crates/ide-diagnostics/src/handlers/base_expr_in_struct_pattern.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};

// Diagnostic: base-expr-in-struct-pattern
//
// This diagnostic is triggered if a struct pattern contains a base expression (`..base`).
pub(crate) fn base_expr_in_struct_pattern(
ctx: &DiagnosticsContext<'_>,
d: &hir::BaseExprInStructPattern,
) -> Diagnostic {
Diagnostic::new_with_syntax_node_ptr(
ctx,
DiagnosticCode::SyntaxError,
"base expressions aren't allowed in struct patterns",
d.node.map(Into::into),
)
.stable()
}

#[cfg(test)]
mod tests {
Copy link
Copy Markdown
Contributor Author

@ada4a ada4a Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, it turns out I've been running the wrong tests while working on this... But I'm actually not sure what to do with these extraneous errors:

  • I can't really ignore syntax errors completely, since the error I'm testing is a syntax error itself
  • I guess I could give in and add annotations for them to the test?

It also looks like no other handlers are for syntax errors, so nowhere to draw inspiration from..

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that if we already emit syntax errors here, we don't need another error.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw the error from rustc is not quite ideal (imo):

error: expected `}`, found `g`
 --> src/lib.rs:4:36
  |
4 |     if let Foo { bar: 0, baz: 0, ..g } = f {}
  |            ---                     ^ expected `}`
  |            |
  |            while parsing the fields for this pattern

but I don't have a strong opinion on this.

In this case, I'd open a V2 PR which updates the fixme comment to say something like "we wanted to emit a diagnostic on this but decided to rely on rustc instead, since this is a syntax error"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ada4a

I don't think the point is to rely on rustc. What happens right now is:

Image

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @lnicola, I don't see where you're getting at with this

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By not adding the diagnostic, we're not relying on rustc, we're relying on our parser error (expected COMMA).

use crate::tests::check_diagnostics;

#[test]
fn spread_variable() {
check_diagnostics(
r#"
struct Foo { bar: u32, baz: u32 }
fn test(f: Foo, g: Foo) {
if let Foo { ..g } = f {}
// ^ error: base expressions aren't allowed in struct patterns
if let Foo { bar: 0, ..g } = f {}
// ^ error: base expressions aren't allowed in struct patterns
if let Foo { bar: 0, baz: 0, ..g } = f {}
// ^ error: base expressions aren't allowed in struct patterns
}
"#,
);
}

#[test]
fn spread_default() {
check_diagnostics(
r#"
struct Foo { bar: u32, baz: u32 }
fn test(f: Foo) {
if let Foo { ..Default::default() } = f {}
// ^^^^^^^^^^^^^^^^^^ error: base expressions aren't allowed in struct patterns
}
"#,
);
}

#[test]
fn spread_struct() {
check_diagnostics(
r#"
struct Foo { bar: u32, baz: u32 }
fn test(f: Foo) {
if let Foo { ..Foo { bar: 0, baz: 0 } } = f {}
// ^^^^^^^^^^^^^^^^^^^^^^ error: base expressions aren't allowed in struct patterns
}
"#,
);
}
}
2 changes: 2 additions & 0 deletions crates/ide-diagnostics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ extern crate rustc_driver as _;
mod handlers {
pub(crate) mod await_outside_of_async;
pub(crate) mod bad_rtn;
pub(crate) mod base_expr_in_struct_pattern;
pub(crate) mod break_outside_of_loop;
pub(crate) mod elided_lifetimes_in_path;
pub(crate) mod expected_function;
Expand Down Expand Up @@ -483,6 +484,7 @@ pub fn semantic_diagnostics(
AnyDiagnostic::GenericDefaultRefersToSelf(d) => handlers::generic_default_refers_to_self::generic_default_refers_to_self(&ctx, &d),
AnyDiagnostic::InvalidLhsOfAssignment(d) => handlers::invalid_lhs_of_assignment::invalid_lhs_of_assignment(&ctx, &d),
AnyDiagnostic::TypeMustBeKnown(d) => handlers::type_must_be_known::type_must_be_known(&ctx, &d),
AnyDiagnostic::BaseExprInStructPattern(d) => handlers::base_expr_in_struct_pattern::base_expr_in_struct_pattern(&ctx, &d),
};
res.push(d)
}
Expand Down
Loading