aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorMarcus Klaas de Vries <[email protected]>2019-01-14 19:56:14 +0000
committerMarcus Klaas de Vries <[email protected]>2019-01-14 19:56:14 +0000
commitd67eabb512a08a451a649c7f20e4e9ae1860a8a0 (patch)
tree99c5fb5004f3fb7a6b0855033855c439675cc0ff /crates
parent26893487722d07b3f31a6addfc88e6948620989c (diff)
Fix type inference for raw (byte) strings
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/ty/tests.rs12
-rw-r--r--crates/ra_hir/src/ty/tests/data/literals.txt4
-rw-r--r--crates/ra_ide_api/src/hover.rs5
-rw-r--r--crates/ra_syntax/src/ast/generated.rs56
-rw-r--r--crates/ra_syntax/src/grammar.ron4
-rw-r--r--crates/ra_syntax/src/lexer/strings.rs15
6 files changed, 75 insertions, 21 deletions
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs
index cbdb2a4b7..8aacb1a7f 100644
--- a/crates/ra_hir/src/ty/tests.rs
+++ b/crates/ra_hir/src/ty/tests.rs
@@ -135,7 +135,7 @@ fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) {
135#[test] 135#[test]
136fn infer_literals() { 136fn infer_literals() {
137 check_inference( 137 check_inference(
138 r#" 138 r##"
139fn test() { 139fn test() {
140 5i32; 140 5i32;
141 "hello"; 141 "hello";
@@ -146,8 +146,14 @@ fn test() {
146 5000; 146 5000;
147 false; 147 false;
148 true; 148 true;
149} 149 r#"
150"#, 150 //! doc
151 // non-doc
152 mod foo {}
153 "#;
154 br#"yolo"#;
155}
156"##,
151 "literals.txt", 157 "literals.txt",
152 ); 158 );
153} 159}
diff --git a/crates/ra_hir/src/ty/tests/data/literals.txt b/crates/ra_hir/src/ty/tests/data/literals.txt
index 6e82f458f..84ee2c11b 100644
--- a/crates/ra_hir/src/ty/tests/data/literals.txt
+++ b/crates/ra_hir/src/ty/tests/data/literals.txt
@@ -1,4 +1,4 @@
1[11; 111) '{ ...rue; }': () 1[11; 201) '{ ...o"#; }': ()
2[17; 21) '5i32': i32 2[17; 21) '5i32': i32
3[27; 34) '"hello"': &str 3[27; 34) '"hello"': &str
4[40; 48) 'b"bytes"': &[u8] 4[40; 48) 'b"bytes"': &[u8]
@@ -8,3 +8,5 @@
8[83; 87) '5000': i32 8[83; 87) '5000': i32
9[93; 98) 'false': bool 9[93; 98) 'false': bool
10[104; 108) 'true': bool 10[104; 108) 'true': bool
11[114; 182) 'r#" ... "#': &str
12[188; 198) 'br#"yolo"#': &[u8]
diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs
index d73c5bc31..107b23833 100644
--- a/crates/ra_ide_api/src/hover.rs
+++ b/crates/ra_ide_api/src/hover.rs
@@ -230,20 +230,19 @@ mod tests {
230 assert_eq!("[unknown]", &type_name); 230 assert_eq!("[unknown]", &type_name);
231 } 231 }
232 232
233 // FIXME: improve type_of to make this work
234 #[test] 233 #[test]
235 fn test_type_of_for_expr_2() { 234 fn test_type_of_for_expr_2() {
236 let (analysis, range) = single_file_with_range( 235 let (analysis, range) = single_file_with_range(
237 " 236 "
238 fn main() { 237 fn main() {
239 let foo: usize = 1; 238 let foo: usize = 1;
240 let bar = <|>1 + foo_test<|>; 239 let bar = <|>1 + foo<|>;
241 } 240 }
242 ", 241 ",
243 ); 242 );
244 243
245 let type_name = analysis.type_of(range).unwrap().unwrap(); 244 let type_name = analysis.type_of(range).unwrap().unwrap();
246 assert_eq!("[unknown]", &type_name); 245 assert_eq!("usize", &type_name);
247 } 246 }
248 247
249} 248}
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs
index cad845ec0..3471d5226 100644
--- a/crates/ra_syntax/src/ast/generated.rs
+++ b/crates/ra_syntax/src/ast/generated.rs
@@ -1422,6 +1422,8 @@ unsafe impl TransparentNewType for LiteralExpr {
1422pub enum LiteralExprKind<'a> { 1422pub enum LiteralExprKind<'a> {
1423 String(&'a String), 1423 String(&'a String),
1424 ByteString(&'a ByteString), 1424 ByteString(&'a ByteString),
1425 RawString(&'a RawString),
1426 RawByteString(&'a RawByteString),
1425 Char(&'a Char), 1427 Char(&'a Char),
1426 Byte(&'a Byte), 1428 Byte(&'a Byte),
1427 IntNumber(&'a IntNumber), 1429 IntNumber(&'a IntNumber),
@@ -1435,6 +1437,8 @@ impl AstNode for LiteralExpr {
1435 match syntax.kind() { 1437 match syntax.kind() {
1436 | STRING 1438 | STRING
1437 | BYTE_STRING 1439 | BYTE_STRING
1440 | RAW_STRING
1441 | RAW_BYTE_STRING
1438 | CHAR 1442 | CHAR
1439 | BYTE 1443 | BYTE
1440 | INT_NUMBER 1444 | INT_NUMBER
@@ -1453,6 +1457,8 @@ impl LiteralExpr {
1453 match self.syntax.kind() { 1457 match self.syntax.kind() {
1454 STRING => LiteralExprKind::String(String::cast(&self.syntax).unwrap()), 1458 STRING => LiteralExprKind::String(String::cast(&self.syntax).unwrap()),
1455 BYTE_STRING => LiteralExprKind::ByteString(ByteString::cast(&self.syntax).unwrap()), 1459 BYTE_STRING => LiteralExprKind::ByteString(ByteString::cast(&self.syntax).unwrap()),
1460 RAW_STRING => LiteralExprKind::RawString(RawString::cast(&self.syntax).unwrap()),
1461 RAW_BYTE_STRING => LiteralExprKind::RawByteString(RawByteString::cast(&self.syntax).unwrap()),
1456 CHAR => LiteralExprKind::Char(Char::cast(&self.syntax).unwrap()), 1462 CHAR => LiteralExprKind::Char(Char::cast(&self.syntax).unwrap()),
1457 BYTE => LiteralExprKind::Byte(Byte::cast(&self.syntax).unwrap()), 1463 BYTE => LiteralExprKind::Byte(Byte::cast(&self.syntax).unwrap()),
1458 INT_NUMBER => LiteralExprKind::IntNumber(IntNumber::cast(&self.syntax).unwrap()), 1464 INT_NUMBER => LiteralExprKind::IntNumber(IntNumber::cast(&self.syntax).unwrap()),
@@ -2543,6 +2549,56 @@ impl AstNode for RangePat {
2543 2549
2544impl RangePat {} 2550impl RangePat {}
2545 2551
2552// RawByteString
2553#[derive(Debug, PartialEq, Eq, Hash)]
2554#[repr(transparent)]
2555pub struct RawByteString {
2556 pub(crate) syntax: SyntaxNode,
2557}
2558unsafe impl TransparentNewType for RawByteString {
2559 type Repr = rowan::SyntaxNode<RaTypes>;
2560}
2561
2562impl AstNode for RawByteString {
2563 fn cast(syntax: &SyntaxNode) -> Option<&Self> {
2564 match syntax.kind() {
2565 RAW_BYTE_STRING => Some(RawByteString::from_repr(syntax.into_repr())),
2566 _ => None,
2567 }
2568 }
2569 fn syntax(&self) -> &SyntaxNode { &self.syntax }
2570 fn to_owned(&self) -> TreeArc<RawByteString> { TreeArc::cast(self.syntax.to_owned()) }
2571}
2572
2573
2574impl ast::AstToken for RawByteString {}
2575impl RawByteString {}
2576
2577// RawString
2578#[derive(Debug, PartialEq, Eq, Hash)]
2579#[repr(transparent)]
2580pub struct RawString {
2581 pub(crate) syntax: SyntaxNode,
2582}
2583unsafe impl TransparentNewType for RawString {
2584 type Repr = rowan::SyntaxNode<RaTypes>;
2585}
2586
2587impl AstNode for RawString {
2588 fn cast(syntax: &SyntaxNode) -> Option<&Self> {
2589 match syntax.kind() {
2590 RAW_STRING => Some(RawString::from_repr(syntax.into_repr())),
2591 _ => None,
2592 }
2593 }
2594 fn syntax(&self) -> &SyntaxNode { &self.syntax }
2595 fn to_owned(&self) -> TreeArc<RawString> { TreeArc::cast(self.syntax.to_owned()) }
2596}
2597
2598
2599impl ast::AstToken for RawString {}
2600impl RawString {}
2601
2546// RefExpr 2602// RefExpr
2547#[derive(Debug, PartialEq, Eq, Hash)] 2603#[derive(Debug, PartialEq, Eq, Hash)]
2548#[repr(transparent)] 2604#[repr(transparent)]
diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron
index 34d2a27d1..bd8c5b411 100644
--- a/crates/ra_syntax/src/grammar.ron
+++ b/crates/ra_syntax/src/grammar.ron
@@ -430,7 +430,9 @@ Grammar(
430 "IntNumber": ( traits: ["AstToken"] ), 430 "IntNumber": ( traits: ["AstToken"] ),
431 "FloatNumber": ( traits: ["AstToken"] ), 431 "FloatNumber": ( traits: ["AstToken"] ),
432 "String": ( traits: ["AstToken"] ), 432 "String": ( traits: ["AstToken"] ),
433 "RawString": ( traits: ["AstToken"] ),
433 "Byte": ( traits: ["AstToken"] ), 434 "Byte": ( traits: ["AstToken"] ),
435 "RawByteString": ( traits: ["AstToken"] ),
434 "ByteString": ( traits: ["AstToken"] ), 436 "ByteString": ( traits: ["AstToken"] ),
435 "Char": ( traits: ["AstToken"] ), 437 "Char": ( traits: ["AstToken"] ),
436 "TrueKw": ( traits: ["AstToken"] ), 438 "TrueKw": ( traits: ["AstToken"] ),
@@ -439,6 +441,8 @@ Grammar(
439 enum: [ 441 enum: [
440 "String", 442 "String",
441 "ByteString", 443 "ByteString",
444 "RawString",
445 "RawByteString",
442 "Char", 446 "Char",
443 "Byte", 447 "Byte",
444 "IntNumber", 448 "IntNumber",
diff --git a/crates/ra_syntax/src/lexer/strings.rs b/crates/ra_syntax/src/lexer/strings.rs
index 5090feae6..0865b7f3b 100644
--- a/crates/ra_syntax/src/lexer/strings.rs
+++ b/crates/ra_syntax/src/lexer/strings.rs
@@ -49,7 +49,7 @@ pub(crate) fn scan_byte_char_or_string(ptr: &mut Ptr) -> SyntaxKind {
49 BYTE_STRING 49 BYTE_STRING
50 } 50 }
51 'r' => { 51 'r' => {
52 scan_raw_byte_string(ptr); 52 scan_raw_string(ptr);
53 RAW_BYTE_STRING 53 RAW_BYTE_STRING
54 } 54 }
55 _ => unreachable!(), 55 _ => unreachable!(),
@@ -108,16 +108,3 @@ fn scan_byte(ptr: &mut Ptr) {
108fn scan_byte_string(ptr: &mut Ptr) { 108fn scan_byte_string(ptr: &mut Ptr) {
109 scan_string(ptr) 109 scan_string(ptr)
110} 110}
111
112fn scan_raw_byte_string(ptr: &mut Ptr) {
113 if !ptr.at('"') {
114 return;
115 }
116 ptr.bump();
117
118 while let Some(c) = ptr.bump() {
119 if c == '"' {
120 return;
121 }
122 }
123}