aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2021-05-07 14:35:02 +0100
committerJonas Schievink <[email protected]>2021-05-07 16:22:54 +0100
commite2b664e9fdecebd967d0c7d31e5ad5a07512b7a6 (patch)
tree2eb25332537713f08117d15ad86210580cd7b00c
parenta8da2ca3a189a3f9a422c38d0a26298dc0a9ce6f (diff)
fix: use raw idents in `make::name{_ref}` with keywords
-rw-r--r--crates/ide/src/diagnostics.rs20
-rw-r--r--crates/syntax/src/ast/make.rs14
2 files changed, 32 insertions, 2 deletions
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs
index b14f908b7..273d8cfbb 100644
--- a/crates/ide/src/diagnostics.rs
+++ b/crates/ide/src/diagnostics.rs
@@ -654,6 +654,26 @@ fn test_fn() {
654 } 654 }
655 655
656 #[test] 656 #[test]
657 fn test_fill_struct_fields_raw_ident() {
658 check_fix(
659 r#"
660struct TestStruct { r#type: u8 }
661
662fn test_fn() {
663 TestStruct { $0 };
664}
665"#,
666 r"
667struct TestStruct { r#type: u8 }
668
669fn test_fn() {
670 TestStruct { r#type: () };
671}
672",
673 );
674 }
675
676 #[test]
657 fn test_fill_struct_fields_no_diagnostic() { 677 fn test_fill_struct_fields_no_diagnostic() {
658 check_no_diagnostics( 678 check_no_diagnostics(
659 r" 679 r"
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs
index 4bcea28cc..f8b508a90 100644
--- a/crates/syntax/src/ast/make.rs
+++ b/crates/syntax/src/ast/make.rs
@@ -15,12 +15,22 @@ use stdx::format_to;
15use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxToken}; 15use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxToken};
16 16
17pub fn name(text: &str) -> ast::Name { 17pub fn name(text: &str) -> ast::Name {
18 ast_from_text(&format!("mod {};", text)) 18 ast_from_text(&format!("mod {}{};", raw_ident_esc(text), text))
19} 19}
20 20
21pub fn name_ref(text: &str) -> ast::NameRef { 21pub fn name_ref(text: &str) -> ast::NameRef {
22 ast_from_text(&format!("fn f() {{ {}; }}", text)) 22 ast_from_text(&format!("fn f() {{ {}{}; }}", raw_ident_esc(text), text))
23} 23}
24
25fn raw_ident_esc(ident: &str) -> &'static str {
26 let is_keyword = parser::SyntaxKind::from_keyword(ident).is_some();
27 if is_keyword && !matches!(ident, "self" | "crate" | "super" | "Self") {
28 "r#"
29 } else {
30 ""
31 }
32}
33
24// FIXME: replace stringly-typed constructor with a family of typed ctors, a-la 34// FIXME: replace stringly-typed constructor with a family of typed ctors, a-la
25// `expr_xxx`. 35// `expr_xxx`.
26pub fn ty(text: &str) -> ast::Type { 36pub fn ty(text: &str) -> ast::Type {