Skip to content

Commit 81c0519

Browse files
authored
Unrolled build for #149427
Rollup merge of #149427 - scrabsha:push-rxkwyumxrrtu, r=jdonszelmann Make the capitalization explicit on keyword misspell error Will help for #149405.
2 parents a463b0e + 85e24b0 commit 81c0519

File tree

5 files changed

+52
-18
lines changed

5 files changed

+52
-18
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3544,6 +3544,8 @@ pub fn detect_confusion_type(sm: &SourceMap, suggested: &str, sp: Span) -> Confu
35443544
let mut has_digit_letter_confusable = false;
35453545
let mut has_other_diff = false;
35463546

3547+
// Letters whose lowercase version is very similar to the uppercase
3548+
// version.
35473549
let ascii_confusables = &['c', 'f', 'i', 'k', 'o', 's', 'u', 'v', 'w', 'x', 'y', 'z'];
35483550

35493551
let digit_letter_confusables = [('0', 'O'), ('1', 'l'), ('5', 'S'), ('8', 'B'), ('9', 'g')];

compiler/rustc_parse/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ parse_keyword_lifetime =
513513
lifetimes cannot use keyword names
514514
515515
parse_kw_bad_case = keyword `{$kw}` is written in the wrong case
516-
.suggestion = write it in the correct case
516+
.suggestion = write it in {$case}
517517
518518
parse_label_inner_attr_does_not_annotate_this = the inner attribute doesn't annotate this {$item}
519519
parse_label_unexpected_token = unexpected token

compiler/rustc_parse/src/errors.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
// ignore-tidy-filelength
22

33
use std::borrow::Cow;
4+
use std::path::PathBuf;
45

56
use rustc_ast::token::Token;
67
use rustc_ast::util::parser::ExprPrecedence;
78
use rustc_ast::{Path, Visibility};
89
use rustc_errors::codes::*;
910
use rustc_errors::{
10-
Applicability, Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level, Subdiagnostic,
11-
SuggestionStyle,
11+
Applicability, Diag, DiagArgValue, DiagCtxtHandle, Diagnostic, EmissionGuarantee, IntoDiagArg,
12+
Level, Subdiagnostic, SuggestionStyle,
1213
};
1314
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
1415
use rustc_session::errors::ExprParenthesesNeeded;
@@ -3341,6 +3342,24 @@ pub(crate) struct KwBadCase<'a> {
33413342
#[suggestion(code = "{kw}", style = "verbose", applicability = "machine-applicable")]
33423343
pub span: Span,
33433344
pub kw: &'a str,
3345+
pub case: Case,
3346+
}
3347+
3348+
pub(crate) enum Case {
3349+
Upper,
3350+
Lower,
3351+
Mixed,
3352+
}
3353+
3354+
impl IntoDiagArg for Case {
3355+
fn into_diag_arg(self, path: &mut Option<PathBuf>) -> DiagArgValue {
3356+
match self {
3357+
Case::Upper => "uppercase",
3358+
Case::Lower => "lowercase",
3359+
Case::Mixed => "the correct case",
3360+
}
3361+
.into_diag_arg(path)
3362+
}
33443363
}
33453364

33463365
#[derive(Diagnostic)]

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,20 @@ impl<'a> Parser<'a> {
606606
// Do an ASCII case-insensitive match, because all keywords are ASCII.
607607
&& ident.as_str().eq_ignore_ascii_case(exp.kw.as_str())
608608
{
609-
self.dcx().emit_err(errors::KwBadCase { span: ident.span, kw: exp.kw.as_str() });
609+
let kw = exp.kw.as_str();
610+
let is_upper = kw.chars().all(char::is_uppercase);
611+
let is_lower = kw.chars().all(char::is_lowercase);
612+
613+
let case = match (is_upper, is_lower) {
614+
(true, true) => {
615+
unreachable!("keyword that is both fully upper- and fully lowercase")
616+
}
617+
(true, false) => errors::Case::Upper,
618+
(false, true) => errors::Case::Lower,
619+
(false, false) => errors::Case::Mixed,
620+
};
621+
622+
self.dcx().emit_err(errors::KwBadCase { span: ident.span, kw, case });
610623
self.bump();
611624
true
612625
} else {

tests/ui/parser/item-kw-case-mismatch.stderr

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: keyword `use` is written in the wrong case
44
LL | Use std::ptr::read;
55
| ^^^
66
|
7-
help: write it in the correct case (notice the capitalization)
7+
help: write it in lowercase (notice the capitalization)
88
|
99
LL - Use std::ptr::read;
1010
LL + use std::ptr::read;
@@ -16,7 +16,7 @@ error: keyword `use` is written in the wrong case
1616
LL | USE std::ptr::write;
1717
| ^^^
1818
|
19-
help: write it in the correct case
19+
help: write it in lowercase
2020
|
2121
LL - USE std::ptr::write;
2222
LL + use std::ptr::write;
@@ -28,7 +28,7 @@ error: keyword `fn` is written in the wrong case
2828
LL | async Fn _a() {}
2929
| ^^
3030
|
31-
help: write it in the correct case (notice the capitalization)
31+
help: write it in lowercase (notice the capitalization)
3232
|
3333
LL - async Fn _a() {}
3434
LL + async fn _a() {}
@@ -40,7 +40,7 @@ error: keyword `fn` is written in the wrong case
4040
LL | Fn _b() {}
4141
| ^^
4242
|
43-
help: write it in the correct case (notice the capitalization)
43+
help: write it in lowercase (notice the capitalization)
4444
|
4545
LL - Fn _b() {}
4646
LL + fn _b() {}
@@ -52,7 +52,7 @@ error: keyword `async` is written in the wrong case
5252
LL | aSYNC fN _c() {}
5353
| ^^^^^
5454
|
55-
help: write it in the correct case
55+
help: write it in lowercase
5656
|
5757
LL - aSYNC fN _c() {}
5858
LL + async fN _c() {}
@@ -64,7 +64,7 @@ error: keyword `fn` is written in the wrong case
6464
LL | aSYNC fN _c() {}
6565
| ^^
6666
|
67-
help: write it in the correct case
67+
help: write it in lowercase
6868
|
6969
LL - aSYNC fN _c() {}
7070
LL + aSYNC fn _c() {}
@@ -76,7 +76,7 @@ error: keyword `async` is written in the wrong case
7676
LL | Async fn _d() {}
7777
| ^^^^^
7878
|
79-
help: write it in the correct case
79+
help: write it in lowercase
8080
|
8181
LL - Async fn _d() {}
8282
LL + async fn _d() {}
@@ -88,7 +88,7 @@ error: keyword `const` is written in the wrong case
8888
LL | CONST UNSAFE FN _e() {}
8989
| ^^^^^
9090
|
91-
help: write it in the correct case
91+
help: write it in lowercase
9292
|
9393
LL - CONST UNSAFE FN _e() {}
9494
LL + const UNSAFE FN _e() {}
@@ -100,7 +100,7 @@ error: keyword `unsafe` is written in the wrong case
100100
LL | CONST UNSAFE FN _e() {}
101101
| ^^^^^^
102102
|
103-
help: write it in the correct case
103+
help: write it in lowercase
104104
|
105105
LL - CONST UNSAFE FN _e() {}
106106
LL + CONST unsafe FN _e() {}
@@ -112,7 +112,7 @@ error: keyword `fn` is written in the wrong case
112112
LL | CONST UNSAFE FN _e() {}
113113
| ^^
114114
|
115-
help: write it in the correct case
115+
help: write it in lowercase
116116
|
117117
LL - CONST UNSAFE FN _e() {}
118118
LL + CONST UNSAFE fn _e() {}
@@ -124,7 +124,7 @@ error: keyword `unsafe` is written in the wrong case
124124
LL | unSAFE EXTern "C" fn _f() {}
125125
| ^^^^^^
126126
|
127-
help: write it in the correct case
127+
help: write it in lowercase
128128
|
129129
LL - unSAFE EXTern "C" fn _f() {}
130130
LL + unsafe EXTern "C" fn _f() {}
@@ -136,7 +136,7 @@ error: keyword `extern` is written in the wrong case
136136
LL | unSAFE EXTern "C" fn _f() {}
137137
| ^^^^^^
138138
|
139-
help: write it in the correct case
139+
help: write it in lowercase
140140
|
141141
LL - unSAFE EXTern "C" fn _f() {}
142142
LL + unSAFE extern "C" fn _f() {}
@@ -148,7 +148,7 @@ error: keyword `extern` is written in the wrong case
148148
LL | EXTERN "C" FN _g() {}
149149
| ^^^^^^
150150
|
151-
help: write it in the correct case
151+
help: write it in lowercase
152152
|
153153
LL - EXTERN "C" FN _g() {}
154154
LL + extern "C" FN _g() {}
@@ -160,7 +160,7 @@ error: keyword `fn` is written in the wrong case
160160
LL | EXTERN "C" FN _g() {}
161161
| ^^
162162
|
163-
help: write it in the correct case
163+
help: write it in lowercase
164164
|
165165
LL - EXTERN "C" FN _g() {}
166166
LL + EXTERN "C" fn _g() {}

0 commit comments

Comments
 (0)