about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Wirth <lukastw97@gmail.com>2022-08-11 10:41:30 +0200
committerLukas Wirth <lukastw97@gmail.com>2022-08-11 10:41:30 +0200
commit1bb58205f0b09ccdb1a8915cfdede35bd2c09da4 (patch)
tree1edbc514c5d6e85be6dcd4a01a0332806b1f942d
parente1e93c44389d822514e89ab4f2f31be30a7126d6 (diff)
downloadrust-1bb58205f0b09ccdb1a8915cfdede35bd2c09da4.tar.gz
rust-1bb58205f0b09ccdb1a8915cfdede35bd2c09da4.zip
Fix panic in no_such_field when using tuple fields on record structs
-rw-r--r--crates/hir/src/source_analyzer.rs1
-rw-r--r--crates/ide-diagnostics/src/handlers/no_such_field.rs18
2 files changed, 17 insertions, 2 deletions
diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs
index f5e2e443070..ae2896e1932 100644
--- a/crates/hir/src/source_analyzer.rs
+++ b/crates/hir/src/source_analyzer.rs
@@ -368,6 +368,7 @@ impl SourceAnalyzer {
         let local = if field.name_ref().is_some() {
             None
         } else {
+            // Shorthand syntax, resolve to the local
             let path = ModPath::from_segments(PathKind::Plain, once(local_name.clone()));
             match self.resolver.resolve_path_in_value_ns_fully(db.upcast(), &path) {
                 Some(ValueNs::LocalBinding(pat_id)) => {
diff --git a/crates/ide-diagnostics/src/handlers/no_such_field.rs b/crates/ide-diagnostics/src/handlers/no_such_field.rs
index e032c578f02..a80299106bd 100644
--- a/crates/ide-diagnostics/src/handlers/no_such_field.rs
+++ b/crates/ide-diagnostics/src/handlers/no_such_field.rs
@@ -68,7 +68,7 @@ fn missing_record_expr_field_fixes(
     }
     let new_field = make::record_field(
         None,
-        make::name(&record_expr_field.field_name()?.text()),
+        make::name(&record_expr_field.field_name()?.ident_token()?.text()),
         make::ty(&new_field_type.display_source_code(sema.db, module.into()).ok()?),
     );
 
@@ -109,7 +109,7 @@ fn missing_record_expr_field_fixes(
 
 #[cfg(test)]
 mod tests {
-    use crate::tests::{check_diagnostics, check_fix};
+    use crate::tests::{check_diagnostics, check_fix, check_no_fix};
 
     #[test]
     fn no_such_field_diagnostics() {
@@ -280,4 +280,18 @@ struct Foo {
 "#,
         )
     }
+
+    #[test]
+    fn test_tuple_field_on_record_struct() {
+        check_no_fix(
+            r#"
+struct Struct {}
+fn main() {
+    Struct {
+        0$0: 0
+    }
+}
+"#,
+        )
+    }
 }