Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 4a10b01

Browse files
committed
Use shorter span for existing ' -> " structured suggestion
1 parent 982918f commit 4a10b01

14 files changed

+51
-41
lines changed

compiler/rustc_infer/src/errors/mod.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,15 +1339,12 @@ pub enum TypeErrorAdditionalDiags {
13391339
span: Span,
13401340
code: String,
13411341
},
1342-
#[suggestion(
1343-
infer_meant_str_literal,
1344-
code = "\"{code}\"",
1345-
applicability = "machine-applicable"
1346-
)]
1342+
#[multipart_suggestion(infer_meant_str_literal, applicability = "machine-applicable")]
13471343
MeantStrLiteral {
1348-
#[primary_span]
1349-
span: Span,
1350-
code: String,
1344+
#[suggestion_part(code = "\"")]
1345+
start: Span,
1346+
#[suggestion_part(code = "\"")]
1347+
end: Span,
13511348
},
13521349
#[suggestion(
13531350
infer_consider_specifying_length,

compiler/rustc_infer/src/infer/error_reporting/mod.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2078,16 +2078,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
20782078
// If a string was expected and the found expression is a character literal,
20792079
// perhaps the user meant to write `"s"` to specify a string literal.
20802080
(ty::Ref(_, r, _), ty::Char) if r.is_str() => {
2081-
if let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span) {
2082-
if let Some(code) =
2083-
code.strip_prefix('\'').and_then(|s| s.strip_suffix('\''))
2084-
{
2085-
suggestions.push(TypeErrorAdditionalDiags::MeantStrLiteral {
2086-
span,
2087-
code: escape_literal(code),
2088-
})
2089-
}
2090-
}
2081+
suggestions.push(TypeErrorAdditionalDiags::MeantStrLiteral {
2082+
start: span.with_hi(span.lo() + BytePos(1)),
2083+
end: span.with_lo(span.hi() - BytePos(1)),
2084+
})
20912085
}
20922086
// For code `if Some(..) = expr `, the type mismatch may be expected `bool` but found `()`,
20932087
// we try to suggest to add the missing `let` for `if let Some(..) = expr`

compiler/rustc_parse/src/errors.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2216,12 +2216,21 @@ pub enum MoreThanOneCharSugg {
22162216
ch: String,
22172217
},
22182218
#[suggestion(parse_use_double_quotes, code = "{sugg}", applicability = "machine-applicable")]
2219-
Quotes {
2219+
QuotesFull {
22202220
#[primary_span]
22212221
span: Span,
22222222
is_byte: bool,
22232223
sugg: String,
22242224
},
2225+
#[multipart_suggestion(parse_use_double_quotes, applicability = "machine-applicable")]
2226+
Quotes {
2227+
#[suggestion_part(code = "{prefix}\"")]
2228+
start: Span,
2229+
#[suggestion_part(code = "\"")]
2230+
end: Span,
2231+
is_byte: bool,
2232+
prefix: &'static str,
2233+
},
22252234
}
22262235

22272236
#[derive(Subdiagnostic)]

compiler/rustc_parse/src/lexer/unescape_error_reporting.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,21 @@ pub(crate) fn emit_unescape_error(
9595
}
9696
escaped.push(c);
9797
}
98-
let sugg = format!("{prefix}\"{escaped}\"");
99-
MoreThanOneCharSugg::Quotes {
100-
span: full_lit_span,
101-
is_byte: mode == Mode::Byte,
102-
sugg,
98+
if escaped.len() != lit.len() {
99+
let sugg = format!("{prefix}\"{escaped}\"");
100+
MoreThanOneCharSugg::QuotesFull {
101+
span: full_lit_span,
102+
is_byte: mode == Mode::Byte,
103+
sugg,
104+
}
105+
} else {
106+
MoreThanOneCharSugg::Quotes {
107+
start: full_lit_span
108+
.with_hi(full_lit_span.lo() + BytePos((prefix.len() + 1) as u32)),
109+
end: full_lit_span.with_lo(full_lit_span.hi() - BytePos(1)),
110+
is_byte: mode == Mode::Byte,
111+
prefix,
112+
}
103113
}
104114
});
105115
dcx.emit_err(UnescapeError::MoreThanOneChar {

tests/ui/inference/str-as-char.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ LL | let _: &str = '\"\"\"';
1818
help: if you meant to write a `str` literal, use double quotes
1919
|
2020
LL | let _: &str = "\"\"\"";
21-
| ~~~~~~~~
21+
| ~ ~
2222

2323
error: character literal may only contain one codepoint
2424
--> $DIR/str-as-char.rs:10:19
@@ -42,7 +42,7 @@ LL | let _: &str = 'a';
4242
help: if you meant to write a `str` literal, use double quotes
4343
|
4444
LL | let _: &str = "a";
45-
| ~~~
45+
| ~ ~
4646

4747
error: aborting due to 4 previous errors
4848

tests/ui/issues/issue-23589.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ LL | let v: Vec(&str) = vec!['1', '2'];
1818
help: if you meant to write a `str` literal, use double quotes
1919
|
2020
LL | let v: Vec(&str) = vec!["1", '2'];
21-
| ~~~
21+
| ~ ~
2222

2323
error: aborting due to 2 previous errors
2424

tests/ui/lexer/lex-bad-char-literals-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | 'nope'
77
help: if you meant to write a `str` literal, use double quotes
88
|
99
LL | "nope"
10-
| ~~~~~~
10+
| ~ ~
1111

1212
error: aborting due to 1 previous error
1313

tests/ui/lexer/lex-bad-char-literals-3.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | static c: char = '●●';
77
help: if you meant to write a `str` literal, use double quotes
88
|
99
LL | static c: char = "●●";
10-
| ~~~~
10+
| ~ ~
1111

1212
error: character literal may only contain one codepoint
1313
--> $DIR/lex-bad-char-literals-3.rs:5:20
@@ -18,7 +18,7 @@ LL | let ch: &str = '●●';
1818
help: if you meant to write a `str` literal, use double quotes
1919
|
2020
LL | let ch: &str = "●●";
21-
| ~~~~
21+
| ~ ~
2222

2323
error: aborting due to 2 previous errors
2424

tests/ui/lexer/lex-bad-char-literals-5.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | static c: char = '\x10\x10';
77
help: if you meant to write a `str` literal, use double quotes
88
|
99
LL | static c: char = "\x10\x10";
10-
| ~~~~~~~~~~
10+
| ~ ~
1111

1212
error: character literal may only contain one codepoint
1313
--> $DIR/lex-bad-char-literals-5.rs:5:20
@@ -18,7 +18,7 @@ LL | let ch: &str = '\x10\x10';
1818
help: if you meant to write a `str` literal, use double quotes
1919
|
2020
LL | let ch: &str = "\x10\x10";
21-
| ~~~~~~~~~~
21+
| ~ ~
2222

2323
error: aborting due to 2 previous errors
2424

tests/ui/lexer/lex-bad-char-literals-6.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | let x: &str = 'ab';
77
help: if you meant to write a `str` literal, use double quotes
88
|
99
LL | let x: &str = "ab";
10-
| ~~~~
10+
| ~ ~
1111

1212
error: character literal may only contain one codepoint
1313
--> $DIR/lex-bad-char-literals-6.rs:4:19
@@ -18,7 +18,7 @@ LL | let y: char = 'cd';
1818
help: if you meant to write a `str` literal, use double quotes
1919
|
2020
LL | let y: char = "cd";
21-
| ~~~~
21+
| ~ ~
2222

2323
error: character literal may only contain one codepoint
2424
--> $DIR/lex-bad-char-literals-6.rs:6:13
@@ -29,7 +29,7 @@ LL | let z = 'ef';
2929
help: if you meant to write a `str` literal, use double quotes
3030
|
3131
LL | let z = "ef";
32-
| ~~~~
32+
| ~ ~
3333

3434
error[E0308]: mismatched types
3535
--> $DIR/lex-bad-char-literals-6.rs:13:20

tests/ui/lexer/lex-bad-str-literal-as-char-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | println!(' 1 + 1');
77
help: if you meant to write a `str` literal, use double quotes
88
|
99
LL | println!(" 1 + 1");
10-
| ~~~~~~~~
10+
| ~ ~
1111

1212
error: aborting due to 1 previous error
1313

tests/ui/parser/issues/issue-64732.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | let _foo = b'hello\0';
77
help: if you meant to write a byte string literal, use double quotes
88
|
99
LL | let _foo = b"hello\0";
10-
| ~~~~~~~~~~
10+
| ~~ ~
1111

1212
error: character literal may only contain one codepoint
1313
--> $DIR/issue-64732.rs:6:16
@@ -18,7 +18,7 @@ LL | let _bar = 'hello';
1818
help: if you meant to write a `str` literal, use double quotes
1919
|
2020
LL | let _bar = "hello";
21-
| ~~~~~~~
21+
| ~ ~
2222

2323
error: aborting due to 2 previous errors
2424

tests/ui/parser/unicode-character-literal.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LL | let _spade = '♠️';
1212
help: if you meant to write a `str` literal, use double quotes
1313
|
1414
LL | let _spade = "♠️";
15-
| ~~~
15+
| ~ ~
1616

1717
error: character literal may only contain one codepoint
1818
--> $DIR/unicode-character-literal.rs:12:14
@@ -28,7 +28,7 @@ LL | let _s = 'ṩ̂̊';
2828
help: if you meant to write a `str` literal, use double quotes
2929
|
3030
LL | let _s = "ṩ̂̊";
31-
| ~~~
31+
| ~ ~
3232

3333
error: character literal may only contain one codepoint
3434
--> $DIR/unicode-character-literal.rs:17:14

tests/ui/str/str-as-char.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | println!('●●');
77
help: if you meant to write a `str` literal, use double quotes
88
|
99
LL | println!("●●");
10-
| ~~~~
10+
| ~ ~
1111

1212
error: aborting due to 1 previous error
1313

0 commit comments

Comments
 (0)