Skip to content

Commit c365329

Browse files
bors[bot]matklad
andauthored
Merge #6486
6486: Cleanup API r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 7f12a1f + 5db789d commit c365329

File tree

5 files changed

+84
-96
lines changed

5 files changed

+84
-96
lines changed

crates/assists/src/handlers/convert_integer_literal.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ use crate::{AssistContext, AssistId, AssistKind, Assists, GroupLabel};
1414
// const _: i32 = 0b1010;
1515
// ```
1616
pub(crate) fn convert_integer_literal(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
17-
let literal = ctx.find_node_at_offset::<ast::Literal>()?.as_int_number()?;
17+
let literal = ctx.find_node_at_offset::<ast::Literal>()?;
18+
let literal = match literal.kind() {
19+
ast::LiteralKind::IntNumber(it) => it,
20+
_ => return None,
21+
};
1822
let radix = literal.radix();
1923
let value = literal.value()?;
2024
let suffix = literal.suffix();

crates/hir_def/src/body/lower.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -953,18 +953,19 @@ impl From<ast::BinOp> for BinaryOp {
953953
impl From<ast::LiteralKind> for Literal {
954954
fn from(ast_lit_kind: ast::LiteralKind) -> Self {
955955
match ast_lit_kind {
956-
LiteralKind::IntNumber { suffix } => {
957-
let known_name = suffix.and_then(|it| BuiltinInt::from_suffix(&it));
958-
959-
Literal::Int(Default::default(), known_name)
956+
LiteralKind::IntNumber(lit) => {
957+
if let Some(float_suffix) = lit.suffix().and_then(BuiltinFloat::from_suffix) {
958+
return Literal::Float(Default::default(), Some(float_suffix));
959+
}
960+
let ty = lit.suffix().and_then(|it| BuiltinInt::from_suffix(&it));
961+
Literal::Int(Default::default(), ty)
960962
}
961-
LiteralKind::FloatNumber { suffix } => {
962-
let known_name = suffix.and_then(|it| BuiltinFloat::from_suffix(&it));
963-
964-
Literal::Float(Default::default(), known_name)
963+
LiteralKind::FloatNumber(lit) => {
964+
let ty = lit.suffix().and_then(|it| BuiltinFloat::from_suffix(&it));
965+
Literal::Float(Default::default(), ty)
965966
}
966-
LiteralKind::ByteString => Literal::ByteString(Default::default()),
967-
LiteralKind::String => Literal::String(Default::default()),
967+
LiteralKind::ByteString(_) => Literal::ByteString(Default::default()),
968+
LiteralKind::String(_) => Literal::String(Default::default()),
968969
LiteralKind::Byte => Literal::Int(Default::default(), Some(BuiltinInt::U8)),
969970
LiteralKind::Bool(val) => Literal::Bool(val),
970971
LiteralKind::Char => Literal::Char(Default::default()),

crates/syntax/src/ast/expr_ext.rs

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::{
44
ast::{self, support, AstChildren, AstNode},
5-
AstToken, SmolStr,
5+
AstToken,
66
SyntaxKind::*,
77
SyntaxToken, T,
88
};
@@ -298,12 +298,12 @@ impl ast::ArrayExpr {
298298

299299
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
300300
pub enum LiteralKind {
301-
String,
302-
ByteString,
301+
String(ast::String),
302+
ByteString(ast::ByteString),
303+
IntNumber(ast::IntNumber),
304+
FloatNumber(ast::FloatNumber),
303305
Char,
304306
Byte,
305-
IntNumber { suffix: Option<SmolStr> },
306-
FloatNumber { suffix: Option<SmolStr> },
307307
Bool(bool),
308308
}
309309

@@ -315,53 +315,25 @@ impl ast::Literal {
315315
.and_then(|e| e.into_token())
316316
.unwrap()
317317
}
318-
319-
pub fn as_int_number(&self) -> Option<ast::IntNumber> {
320-
ast::IntNumber::cast(self.token())
321-
}
322-
323-
pub fn as_string(&self) -> Option<ast::String> {
324-
ast::String::cast(self.token())
325-
}
326-
pub fn as_byte_string(&self) -> Option<ast::ByteString> {
327-
ast::ByteString::cast(self.token())
328-
}
329-
330-
fn find_suffix(text: &str, possible_suffixes: &[&str]) -> Option<SmolStr> {
331-
possible_suffixes
332-
.iter()
333-
.find(|&suffix| text.ends_with(suffix))
334-
.map(|&suffix| SmolStr::new(suffix))
335-
}
336-
337318
pub fn kind(&self) -> LiteralKind {
338319
let token = self.token();
339320

321+
if let Some(t) = ast::IntNumber::cast(token.clone()) {
322+
return LiteralKind::IntNumber(t);
323+
}
324+
if let Some(t) = ast::FloatNumber::cast(token.clone()) {
325+
return LiteralKind::FloatNumber(t);
326+
}
327+
if let Some(t) = ast::String::cast(token.clone()) {
328+
return LiteralKind::String(t);
329+
}
330+
if let Some(t) = ast::ByteString::cast(token.clone()) {
331+
return LiteralKind::ByteString(t);
332+
}
333+
340334
match token.kind() {
341-
INT_NUMBER => {
342-
// FYI: there was a bug here previously, thus the if statement below is necessary.
343-
// The lexer treats e.g. `1f64` as an integer literal. See
344-
// https://github.com/rust-analyzer/rust-analyzer/issues/1592
345-
// and the comments on the linked PR.
346-
let text = token.text();
347-
if let suffix @ Some(_) = Self::find_suffix(&text, &ast::FloatNumber::SUFFIXES) {
348-
LiteralKind::FloatNumber { suffix }
349-
} else {
350-
LiteralKind::IntNumber {
351-
suffix: Self::find_suffix(&text, &ast::IntNumber::SUFFIXES),
352-
}
353-
}
354-
}
355-
FLOAT_NUMBER => {
356-
let text = token.text();
357-
LiteralKind::FloatNumber {
358-
suffix: Self::find_suffix(&text, &ast::FloatNumber::SUFFIXES),
359-
}
360-
}
361-
STRING => LiteralKind::String,
362335
T![true] => LiteralKind::Bool(true),
363336
T![false] => LiteralKind::Bool(false),
364-
BYTE_STRING => LiteralKind::ByteString,
365337
CHAR => LiteralKind::Char,
366338
BYTE => LiteralKind::Byte,
367339
_ => unreachable!(),

crates/syntax/src/ast/token_ext.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -517,10 +517,9 @@ impl HasFormatSpecifier for ast::String {
517517
}
518518

519519
impl ast::IntNumber {
520-
#[rustfmt::skip]
521-
pub(crate) const SUFFIXES: &'static [&'static str] = &[
522-
"u8", "u16", "u32", "u64", "u128", "usize",
523-
"i8", "i16", "i32", "i64", "i128", "isize",
520+
const SUFFIXES: &'static [&'static str] = &[
521+
"u8", "u16", "u32", "u64", "u128", "usize", // Unsigned.
522+
"i8", "i16", "i32", "i64", "i128", "isize", // Signed.
524523
];
525524

526525
pub fn radix(&self) -> Radix {
@@ -555,9 +554,24 @@ impl ast::IntNumber {
555554

556555
pub fn suffix(&self) -> Option<&str> {
557556
let text = self.text();
558-
// FIXME: don't check a fixed set of suffixes, `1_0_1___lol` is valid
559-
// syntax, suffix is `lol`.
560-
ast::IntNumber::SUFFIXES.iter().find_map(|suffix| {
557+
// FIXME: don't check a fixed set of suffixes, `1_0_1_l_o_l` is valid
558+
// syntax, suffix is `l_o_l`.
559+
ast::IntNumber::SUFFIXES.iter().chain(ast::FloatNumber::SUFFIXES.iter()).find_map(
560+
|suffix| {
561+
if text.ends_with(suffix) {
562+
return Some(&text[text.len() - suffix.len()..]);
563+
}
564+
None
565+
},
566+
)
567+
}
568+
}
569+
570+
impl ast::FloatNumber {
571+
const SUFFIXES: &'static [&'static str] = &["f32", "f64"];
572+
pub fn suffix(&self) -> Option<&str> {
573+
let text = self.text();
574+
ast::FloatNumber::SUFFIXES.iter().find_map(|suffix| {
561575
if text.ends_with(suffix) {
562576
return Some(&text[text.len() - suffix.len()..]);
563577
}
@@ -566,10 +580,6 @@ impl ast::IntNumber {
566580
}
567581
}
568582

569-
impl ast::FloatNumber {
570-
pub(crate) const SUFFIXES: &'static [&'static str] = &["f32", "f64"];
571-
}
572-
573583
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
574584
pub enum Radix {
575585
Binary = 2,

crates/syntax/src/validation.rs

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod block;
44

55
use crate::{
66
algo, ast, match_ast, AstNode, SyntaxError,
7-
SyntaxKind::{BYTE, CHAR, CONST, FN, INT_NUMBER, TYPE_ALIAS},
7+
SyntaxKind::{CONST, FN, INT_NUMBER, TYPE_ALIAS},
88
SyntaxNode, SyntaxToken, TextSize, T,
99
};
1010
use rowan::Direction;
@@ -121,41 +121,42 @@ fn validate_literal(literal: ast::Literal, acc: &mut Vec<SyntaxError>) {
121121
acc.push(SyntaxError::new_at_offset(rustc_unescape_error_to_string(err), off));
122122
};
123123

124-
if let Some(s) = literal.as_string() {
125-
if !s.is_raw() {
126-
if let Some(without_quotes) = unquote(text, 1, '"') {
127-
unescape_literal(without_quotes, Mode::Str, &mut |range, char| {
128-
if let Err(err) = char {
129-
push_err(1, (range.start, err));
130-
}
131-
})
132-
}
133-
}
134-
}
135-
if let Some(s) = literal.as_byte_string() {
136-
if !s.is_raw() {
137-
if let Some(without_quotes) = unquote(text, 2, '"') {
138-
unescape_byte_literal(without_quotes, Mode::ByteStr, &mut |range, char| {
139-
if let Err(err) = char {
140-
push_err(2, (range.start, err));
141-
}
142-
})
124+
match literal.kind() {
125+
ast::LiteralKind::String(s) => {
126+
if !s.is_raw() {
127+
if let Some(without_quotes) = unquote(text, 1, '"') {
128+
unescape_literal(without_quotes, Mode::Str, &mut |range, char| {
129+
if let Err(err) = char {
130+
push_err(1, (range.start, err));
131+
}
132+
})
133+
}
143134
}
144135
}
145-
}
146-
147-
match token.kind() {
148-
BYTE => {
149-
if let Some(Err(e)) = unquote(text, 2, '\'').map(unescape_byte) {
150-
push_err(2, e);
136+
ast::LiteralKind::ByteString(s) => {
137+
if !s.is_raw() {
138+
if let Some(without_quotes) = unquote(text, 2, '"') {
139+
unescape_byte_literal(without_quotes, Mode::ByteStr, &mut |range, char| {
140+
if let Err(err) = char {
141+
push_err(2, (range.start, err));
142+
}
143+
})
144+
}
151145
}
152146
}
153-
CHAR => {
147+
ast::LiteralKind::Char => {
154148
if let Some(Err(e)) = unquote(text, 1, '\'').map(unescape_char) {
155149
push_err(1, e);
156150
}
157151
}
158-
_ => (),
152+
ast::LiteralKind::Byte => {
153+
if let Some(Err(e)) = unquote(text, 2, '\'').map(unescape_byte) {
154+
push_err(2, e);
155+
}
156+
}
157+
ast::LiteralKind::IntNumber(_)
158+
| ast::LiteralKind::FloatNumber(_)
159+
| ast::LiteralKind::Bool(_) => {}
159160
}
160161
}
161162

0 commit comments

Comments
 (0)