Skip to content

Commit 4f6b2a2

Browse files
committed
Auto merge of rust-lang#12241 - jonas-schievink:does-not-float-my-boat, r=jonas-schievink
fix: revert float parsing "fix" to avoid macro-related panics Reverts rust-lang/rust-analyzer#12149 and the follow-up fixes, while keeping their tests. rust-lang/rust-analyzer#12149 has caused many unexpected panics related to macros, and the fixes for those are not straightforward and further complicate the MBE token conversion logic, which was already fairly hard to follow before these fixes.
2 parents 0f9ffb5 + 0831f31 commit 4f6b2a2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+152
-606
lines changed

crates/hir-def/src/body/lower.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ impl From<ast::LiteralKind> for Literal {
972972
}
973973
}
974974
LiteralKind::FloatNumber(lit) => {
975-
let ty = lit.suffix().and_then(|s| BuiltinFloat::from_suffix(&s));
975+
let ty = lit.suffix().and_then(BuiltinFloat::from_suffix);
976976
Literal::Float(Default::default(), ty)
977977
}
978978
LiteralKind::ByteString(bs) => {

crates/hir-def/src/macro_expansion_tests/mbe.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ struct#10 MyTraitMap2#32 {#13
4848

4949
#[test]
5050
fn token_mapping_floats() {
51+
// Regression test for https://github.com/rust-lang/rust-analyzer/issues/12216
52+
// (and related issues)
5153
check(
5254
r#"
5355
// +tokenids
@@ -87,9 +89,9 @@ macro_rules! f {#0
8789
// }
8890
fn#19 main#20(#21)#21 {#22
8991
1#23;#24
90-
1#26.0;
91-
let x#31 =#22 1;
92-
}
92+
1.0#25;#26
93+
let#27 x#28 =#29 1#30;#31
94+
}#22
9395
9496
9597
"##]],

crates/hir-def/src/macro_expansion_tests/mbe/meta_syntax.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ macro_rules! f3 { ($i:_) => () }
8080

8181
#[test]
8282
fn test_rustc_issue_57597() {
83-
// <https://github.com/rust-lang/rust/blob/master/src/test/ui/macros/issue-57597.rs>
83+
// <https://github.com/rust-lang/rust/blob/master/src/test/ui/issues/issue-57597.rs>
8484
check(
8585
r#"
8686
macro_rules! m0 { ($($($i:ident)?)+) => {}; }

crates/hir-def/src/macro_expansion_tests/mbe/tt_conversion.rs

+22-29
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ macro_rules! m {
3838
let _ = 12E+99_f64;
3939
let _ = "rust1";
4040
let _ = -92;
41-
let _ = -1.3e4f32;
4241
}
4342
}
4443
fn f() {
@@ -53,7 +52,6 @@ macro_rules! m {
5352
let _ = 12E+99_f64;
5453
let _ = "rust1";
5554
let _ = -92;
56-
let _ = -1.3e4f32;
5755
}
5856
}
5957
fn f() {
@@ -62,7 +60,6 @@ fn f() {
6260
let _ = 12E+99_f64;
6361
let _ = "rust1";
6462
let _ = -92;
65-
let _ = -1.3e4f32;
6663
}
6764
"#]],
6865
);
@@ -153,55 +150,51 @@ $ = ();
153150
}
154151

155152
#[test]
156-
fn float_literal_in_output() {
153+
fn float_literal_in_tt() {
157154
check(
158155
r#"
159156
macro_rules! constant {
160-
($e:expr ;) => {$e};
157+
($( $ret:expr; )*) => {};
161158
}
162-
163-
const _: () = constant!(0.0;);
164-
const _: () = constant!(0.;);
165-
const _: () = constant!(0e0;);
159+
macro_rules! float_const_impl {
160+
() => ( constant!(0.3; 3.3;); );
161+
}
162+
float_const_impl! {}
166163
"#,
167164
expect![[r#"
168165
macro_rules! constant {
169-
($e:expr ;) => {$e};
166+
($( $ret:expr; )*) => {};
170167
}
171-
172-
const _: () = 0.0;
173-
const _: () = 0.;
174-
const _: () = 0e0;
168+
macro_rules! float_const_impl {
169+
() => ( constant!(0.3; 3.3;); );
170+
}
171+
constant!(0.3;
172+
3.3;
173+
);
175174
"#]],
176175
);
177176
}
178177

179178
#[test]
180-
fn float_literal_in_tt() {
179+
fn float_literal_in_output() {
181180
check(
182181
r#"
183182
macro_rules! constant {
184-
($( $ret:expr; )*) => {};
185-
}
186-
187-
macro_rules! float_const_impl {
188-
() => ( constant!(0.3; 3.3;); );
183+
($e:expr ;) => {$e};
189184
}
190185
191-
float_const_impl! {}
186+
const _: () = constant!(0.0;);
187+
const _: () = constant!(0.;);
188+
const _: () = constant!(0e0;);
192189
"#,
193190
expect![[r#"
194191
macro_rules! constant {
195-
($( $ret:expr; )*) => {};
196-
}
197-
198-
macro_rules! float_const_impl {
199-
() => ( constant!(0.3; 3.3;); );
192+
($e:expr ;) => {$e};
200193
}
201194
202-
constant!(0.3;
203-
3.3;
204-
);
195+
const _: () = 0.0;
196+
const _: () = 0.;
197+
const _: () = 0e0;
205198
"#]],
206199
);
207200
}

crates/hir-def/src/macro_expansion_tests/proc_macros.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ macro_rules! id {
104104
$($t)*
105105
};
106106
}
107-
108-
id! {
107+
id /*+errors*/! {
109108
#[proc_macros::identity]
110109
impl Foo for WrapBj {
111110
async fn foo(&self) {
@@ -120,7 +119,7 @@ macro_rules! id {
120119
$($t)*
121120
};
122121
}
123-
122+
/* parse error: expected SEMICOLON */
124123
#[proc_macros::identity] impl Foo for WrapBj {
125124
async fn foo(&self ) {
126125
self .0.id().await ;

crates/hir-expand/src/builtin_fn_macro.rs

+21-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use base_db::{AnchoredPath, Edition, FileId};
44
use cfg::CfgExpr;
55
use either::Either;
66
use mbe::{parse_exprs_with_sep, parse_to_token_tree};
7-
use syntax::{ast, SmolStr};
7+
use syntax::{
8+
ast::{self, AstToken},
9+
SmolStr,
10+
};
811

912
use crate::{db::AstDatabase, name, quote, ExpandError, ExpandResult, MacroCallId, MacroCallLoc};
1013

@@ -355,7 +358,14 @@ fn unreachable_expand(
355358
}
356359

357360
fn unquote_str(lit: &tt::Literal) -> Option<String> {
358-
let token = ast::make::literal(&lit.to_string()).as_string()?;
361+
let lit = ast::make::tokens::literal(&lit.to_string());
362+
let token = ast::String::cast(lit)?;
363+
token.value().map(|it| it.into_owned())
364+
}
365+
366+
fn unquote_byte_string(lit: &tt::Literal) -> Option<Vec<u8>> {
367+
let lit = ast::make::tokens::literal(&lit.to_string());
368+
let token = ast::ByteString::cast(lit)?;
359369
token.value().map(|it| it.into_owned())
360370
}
361371

@@ -432,16 +442,12 @@ fn concat_bytes_expand(
432442
for (i, t) in tt.token_trees.iter().enumerate() {
433443
match t {
434444
tt::TokenTree::Leaf(tt::Leaf::Literal(lit)) => {
435-
let lit = ast::make::literal(&lit.to_string());
436-
match lit.kind() {
437-
ast::LiteralKind::ByteString(s) => {
438-
s.value()
439-
.unwrap_or_default()
440-
.into_iter()
441-
.for_each(|x| bytes.push(x.to_string()));
442-
}
443-
ast::LiteralKind::Byte(_) => {
444-
bytes.push(lit.to_string());
445+
let token = ast::make::tokens::literal(&lit.to_string());
446+
match token.kind() {
447+
syntax::SyntaxKind::BYTE => bytes.push(token.text().to_string()),
448+
syntax::SyntaxKind::BYTE_STRING => {
449+
let components = unquote_byte_string(lit).unwrap_or_else(Vec::new);
450+
components.into_iter().for_each(|x| bytes.push(x.to_string()));
445451
}
446452
_ => {
447453
err.get_or_insert(mbe::ExpandError::UnexpectedToken.into());
@@ -475,10 +481,10 @@ fn concat_bytes_expand_subtree(
475481
for (ti, tt) in tree.token_trees.iter().enumerate() {
476482
match tt {
477483
tt::TokenTree::Leaf(tt::Leaf::Literal(lit)) => {
478-
let lit = ast::make::literal(&lit.to_string());
484+
let lit = ast::make::tokens::literal(&lit.to_string());
479485
match lit.kind() {
480-
ast::LiteralKind::IntNumber(_) | ast::LiteralKind::Byte(_) => {
481-
bytes.push(lit.to_string());
486+
syntax::SyntaxKind::BYTE | syntax::SyntaxKind::INT_NUMBER => {
487+
bytes.push(lit.text().to_string())
482488
}
483489
_ => {
484490
return Err(mbe::ExpandError::UnexpectedToken.into());

crates/hir-ty/src/tests/simple.rs

-11
Original file line numberDiff line numberDiff line change
@@ -2733,14 +2733,3 @@ fn f() {
27332733
"#,
27342734
);
27352735
}
2736-
2737-
#[test]
2738-
fn nested_tuple_index() {
2739-
check_no_mismatches(
2740-
r#"
2741-
fn main() {
2742-
let fld: i32 = ((0,),).0.0;
2743-
}
2744-
"#,
2745-
);
2746-
}

crates/ide-completion/src/completions/dot.rs

-20
Original file line numberDiff line numberDiff line change
@@ -793,24 +793,4 @@ fn main() {
793793
",
794794
)
795795
}
796-
797-
#[test]
798-
fn tuple_index_completion() {
799-
check(
800-
r#"
801-
struct I;
802-
impl I {
803-
fn i_method(&self) {}
804-
}
805-
struct S((), I);
806-
807-
fn f(s: S) {
808-
s.1.$0
809-
}
810-
"#,
811-
expect![[r#"
812-
me i_method() fn(&self)
813-
"#]],
814-
);
815-
}
816796
}

crates/ide-completion/src/completions/postfix.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod format_like;
55
use hir::{Documentation, HasAttrs};
66
use ide_db::{imports::insert_use::ImportScope, ty_filter::TryEnum, SnippetCap};
77
use syntax::{
8-
ast::{self, AstNode, LiteralKind},
8+
ast::{self, AstNode, AstToken},
99
SyntaxKind::{EXPR_STMT, STMT_LIST},
1010
TextRange, TextSize,
1111
};
@@ -194,7 +194,7 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
194194
}
195195

196196
if let ast::Expr::Literal(literal) = dot_receiver.clone() {
197-
if let LiteralKind::String(literal_text) = literal.kind() {
197+
if let Some(literal_text) = ast::String::cast(literal.token()) {
198198
add_format_like_completions(acc, ctx, &dot_receiver, cap, &literal_text);
199199
}
200200
}

crates/ide-completion/src/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@ impl<'a> CompletionContext<'a> {
10621062
let receiver_is_ambiguous_float_literal = match &receiver {
10631063
Some(ast::Expr::Literal(l)) => matches! {
10641064
l.kind(),
1065-
ast::LiteralKind::FloatNumber { .. } if l.syntax().last_token().map_or(false, |it| it.kind() == T![.])
1065+
ast::LiteralKind::FloatNumber { .. } if l.syntax().last_token().map_or(false, |it| it.text().ends_with('.'))
10661066
},
10671067
_ => false,
10681068
};

crates/ide/src/syntax_highlighting/highlight.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,7 @@ pub(super) fn token(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Optio
3030
INT_NUMBER if token.ancestors().nth(1).map(|it| it.kind()) == Some(FIELD_EXPR) => {
3131
SymbolKind::Field.into()
3232
}
33-
INT_NUMBER | FLOAT_NUMBER_PART | FLOAT_NUMBER_START_0 | FLOAT_NUMBER_START_1
34-
| FLOAT_NUMBER_START_2 => HlTag::NumericLiteral.into(),
35-
DOT if matches!(
36-
token.prev_token().map(|n| n.kind()),
37-
Some(FLOAT_NUMBER_START_1 | FLOAT_NUMBER_START_2)
38-
) =>
39-
{
40-
HlTag::NumericLiteral.into()
41-
}
33+
INT_NUMBER | FLOAT_NUMBER => HlTag::NumericLiteral.into(),
4234
BYTE => HlTag::ByteLiteral.into(),
4335
CHAR => HlTag::CharLiteral.into(),
4436
IDENT if token.parent().and_then(ast::TokenTree::cast).is_some() => {

0 commit comments

Comments
 (0)