Skip to content

Commit 47d70e1

Browse files
committed
Look for typos when reporting an unknown nightly feature
1 parent 29e035e commit 47d70e1

File tree

8 files changed

+118
-23
lines changed

8 files changed

+118
-23
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use rustc_errors::{
1616
pluralize,
1717
};
1818
use rustc_session::errors::ExprParenthesesNeeded;
19-
use rustc_span::edit_distance::find_best_match_for_name;
2019
use rustc_span::source_map::Spanned;
2120
use rustc_span::symbol::used_keywords;
2221
use rustc_span::{BytePos, DUMMY_SP, Ident, Span, SpanSnippetError, Symbol, kw, sym};
@@ -229,20 +228,15 @@ struct MisspelledKw {
229228
}
230229

231230
/// Checks if the given `lookup` identifier is similar to any keyword symbol in `candidates`.
231+
///
232+
/// This is a specialized version of [`Symbol::find_similar`] that constructs an error when a
233+
/// candidate is found.
232234
fn find_similar_kw(lookup: Ident, candidates: &[Symbol]) -> Option<MisspelledKw> {
233-
let lowercase = lookup.name.as_str().to_lowercase();
234-
let lowercase_sym = Symbol::intern(&lowercase);
235-
if candidates.contains(&lowercase_sym) {
236-
Some(MisspelledKw { similar_kw: lowercase, span: lookup.span, is_incorrect_case: true })
237-
} else if let Some(similar_sym) = find_best_match_for_name(candidates, lookup.name, None) {
238-
Some(MisspelledKw {
239-
similar_kw: similar_sym.to_string(),
240-
span: lookup.span,
241-
is_incorrect_case: false,
242-
})
243-
} else {
244-
None
245-
}
235+
lookup.name.find_similar(candidates).map(|(symbol, is_incorrect_case)| MisspelledKw {
236+
similar_kw: symbol.to_string(),
237+
is_incorrect_case,
238+
span: lookup.span,
239+
})
246240
}
247241

248242
struct MultiSugg {

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pub mod attr;
22
mod attr_wrapper;
3-
mod diagnostics;
3+
pub(crate) mod diagnostics;
44
mod expr;
55
mod generics;
66
mod item;

compiler/rustc_passes/messages.ftl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,10 @@ passes_missing_panic_handler =
420420
passes_missing_stability_attr =
421421
{$descr} has missing stability attribute
422422
423+
passes_misspelled_feature =
424+
unknown feature `{$misspelled_name}`
425+
.suggestion = there is a feature with a similar name: `{$actual_name}`
426+
423427
passes_mixed_export_name_and_no_mangle = `{$no_mangle_attr}` attribute may not be used in combination with `{$export_name_attr}`
424428
.label = `{$no_mangle_attr}` is ignored
425429
.note = `{$export_name_attr}` takes precedence

compiler/rustc_passes/src/errors.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,17 @@ pub(crate) struct UnknownFeature {
11851185
pub feature: Symbol,
11861186
}
11871187

1188+
#[derive(Diagnostic)]
1189+
#[diag(passes_misspelled_feature, code = E0635)]
1190+
pub(crate) struct MisspelledFeature {
1191+
#[primary_span]
1192+
pub span: Span,
1193+
pub misspelled_name: Symbol,
1194+
pub actual_name: Symbol,
1195+
#[suggestion(style = "verbose", code = "{actual_name}", applicability = "maybe-incorrect")]
1196+
pub suggestion: Span,
1197+
}
1198+
11881199
#[derive(Diagnostic)]
11891200
#[diag(passes_unknown_feature_alias, code = E0635)]
11901201
pub(crate) struct RenamedFeature {

compiler/rustc_passes/src/stability.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::num::NonZero;
66
use rustc_ast_lowering::stability::extern_abi_stability;
77
use rustc_data_structures::fx::FxIndexMap;
88
use rustc_data_structures::unord::{ExtendUnord, UnordMap, UnordSet};
9-
use rustc_feature::{EnabledLangFeature, EnabledLibFeature};
9+
use rustc_feature::{EnabledLangFeature, EnabledLibFeature, UNSTABLE_LANG_FEATURES};
1010
use rustc_hir::attrs::{AttributeKind, DeprecatedSince};
1111
use rustc_hir::def::{DefKind, Res};
1212
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId, LocalModDefId};
@@ -1093,8 +1093,36 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
10931093
}
10941094
}
10951095

1096-
for (feature, span) in remaining_lib_features {
1097-
tcx.dcx().emit_err(errors::UnknownFeature { span, feature });
1096+
if !remaining_lib_features.is_empty() {
1097+
let lang_features =
1098+
UNSTABLE_LANG_FEATURES.iter().map(|feature| feature.name).collect::<Vec<_>>();
1099+
let lib_features = tcx
1100+
.crates(())
1101+
.into_iter()
1102+
.flat_map(|&cnum| {
1103+
tcx.lib_features(cnum).stability.keys().copied().into_sorted_stable_ord()
1104+
})
1105+
.collect::<Vec<_>>();
1106+
1107+
let valid_feature_names = [lang_features, lib_features].concat();
1108+
1109+
for (feature, span) in remaining_lib_features {
1110+
let suggestion = feature.find_similar(&valid_feature_names);
1111+
match suggestion {
1112+
Some((actual_name, _)) => {
1113+
let misspelled_name = feature;
1114+
tcx.dcx().emit_err(errors::MisspelledFeature {
1115+
span,
1116+
misspelled_name,
1117+
actual_name,
1118+
suggestion: span,
1119+
});
1120+
}
1121+
None => {
1122+
tcx.dcx().emit_err(errors::UnknownFeature { span, feature });
1123+
}
1124+
}
1125+
}
10981126
}
10991127

11001128
for (&implied_by, &feature) in remaining_implications.to_sorted_stable_ord() {

compiler/rustc_span/src/symbol.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_data_structures::stable_hasher::{
1414
use rustc_data_structures::sync::Lock;
1515
use rustc_macros::{Decodable, Encodable, HashStable_Generic, symbols};
1616

17+
use crate::edit_distance::find_best_match_for_name;
1718
use crate::{DUMMY_SP, Edition, Span, with_session_globals};
1819

1920
#[cfg(test)]
@@ -2843,6 +2844,27 @@ impl Symbol {
28432844
// Avoid creating an empty identifier, because that asserts in debug builds.
28442845
if self == sym::empty { String::new() } else { Ident::with_dummy_span(self).to_string() }
28452846
}
2847+
2848+
/// Checks if `self` is similar to any symbol in `candidates`.
2849+
///
2850+
/// The returned boolean represents whether the candidate is the same symbol with a different
2851+
/// casing.
2852+
///
2853+
/// All the candidates are assumed to be lowercase.
2854+
pub fn find_similar(
2855+
self,
2856+
candidates: &[Symbol],
2857+
) -> Option<(Symbol, /* is incorrect case */ bool)> {
2858+
let lowercase = self.as_str().to_lowercase();
2859+
let lowercase_sym = Symbol::intern(&lowercase);
2860+
if candidates.contains(&lowercase_sym) {
2861+
Some((lowercase_sym, true))
2862+
} else if let Some(similar_sym) = find_best_match_for_name(candidates, self, None) {
2863+
Some((similar_sym, false))
2864+
} else {
2865+
None
2866+
}
2867+
}
28462868
}
28472869

28482870
impl fmt::Debug for Symbol {
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1-
#![feature(unknown_rust_feature)] //~ ERROR unknown feature
1+
#![feature(
2+
unknown_rust_feature, //~ ERROR unknown feature
3+
4+
// Typo for lang feature
5+
associated_types_default,
6+
//~^ ERROR unknown feature
7+
//~| HELP there is a feature with a similar name
8+
9+
// Typo for lib feature
10+
core_intrnisics,
11+
//~^ ERROR unknown feature
12+
//~| HELP there is a feature with a similar name
13+
)]
214

315
fn main() {}
Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
error[E0635]: unknown feature `unknown_rust_feature`
2-
--> $DIR/unknown-feature.rs:1:12
2+
--> $DIR/unknown-feature.rs:2:5
33
|
4-
LL | #![feature(unknown_rust_feature)]
5-
| ^^^^^^^^^^^^^^^^^^^^
4+
LL | unknown_rust_feature,
5+
| ^^^^^^^^^^^^^^^^^^^^
66

7-
error: aborting due to 1 previous error
7+
error[E0635]: unknown feature `associated_types_default`
8+
--> $DIR/unknown-feature.rs:5:5
9+
|
10+
LL | associated_types_default,
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^
12+
|
13+
help: there is a feature with a similar name: `associated_type_defaults`
14+
|
15+
LL - associated_types_default,
16+
LL + associated_type_defaults,
17+
|
18+
19+
error[E0635]: unknown feature `core_intrnisics`
20+
--> $DIR/unknown-feature.rs:10:5
21+
|
22+
LL | core_intrnisics,
23+
| ^^^^^^^^^^^^^^^
24+
|
25+
help: there is a feature with a similar name: `core_intrinsics`
26+
|
27+
LL - core_intrnisics,
28+
LL + core_intrinsics,
29+
|
30+
31+
error: aborting due to 3 previous errors
832

933
For more information about this error, try `rustc --explain E0635`.

0 commit comments

Comments
 (0)