diff options
| author | Dylan DPC <dylan.dpc@gmail.com> | 2021-03-15 16:22:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-15 16:22:50 +0100 |
| commit | 4eca4929ec77b34a274a3090310247a1ec485953 (patch) | |
| tree | db7a2890d32962a18fa40e9da3b3cf2453c11a15 /compiler/rustc_resolve | |
| parent | 7a7bbdb3abfa72ca717ef54ffc4f307d0d44de67 (diff) | |
| parent | 5eae9af193efca654a4e20ec0766781c24b7eb87 (diff) | |
Rollup merge of #82989 - Smittyvb:other-lang-literal-errors, r=varkor
Custom error on literal names from other languages This detects all Java literal types and all single word C data types, and suggests the corresponding Rust literal type.
Diffstat (limited to 'compiler/rustc_resolve')
| -rw-r--r-- | compiler/rustc_resolve/src/late/diagnostics.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index e7b3d459766..e85d78db22c 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -563,6 +563,15 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { } } } + } else if err_code == &rustc_errors::error_code!(E0412) { + if let Some(correct) = Self::likely_rust_type(path) { + err.span_suggestion( + span, + "perhaps you intended to use this type", + correct.to_string(), + Applicability::MaybeIncorrect, + ); + } } } @@ -1243,6 +1252,23 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { } } + // Returns the name of the Rust type approximately corresponding to + // a type name in another programming language. + fn likely_rust_type(path: &[Segment]) -> Option<Symbol> { + let name = path[path.len() - 1].ident.as_str(); + // Common Java types + Some(match &*name { + "byte" => sym::u8, // In Java, bytes are signed, but in practice one almost always wants unsigned bytes. + "short" => sym::i16, + "boolean" => sym::bool, + "int" => sym::i32, + "long" => sym::i64, + "float" => sym::f32, + "double" => sym::f64, + _ => return None, + }) + } + /// Only used in a specific case of type ascription suggestions fn get_colon_suggestion_span(&self, start: Span) -> Span { let sm = self.r.session.source_map(); |
