diff options
| author | Takayuki Maeda <takoyaki0316@gmail.com> | 2022-07-12 13:59:51 +0900 |
|---|---|---|
| committer | Takayuki Maeda <takoyaki0316@gmail.com> | 2022-07-12 13:59:51 +0900 |
| commit | e03cb7fb9a10bffbec3ccc0a0ca68c6c97112ea8 (patch) | |
| tree | 74b83230ec0df99db133fd49b2d3138956552914 /compiler | |
| parent | 6f65b7e64be256df673bca2d6cc1f0e461b9eea7 (diff) | |
| download | rust-e03cb7fb9a10bffbec3ccc0a0ca68c6c97112ea8.tar.gz rust-e03cb7fb9a10bffbec3ccc0a0ca68c6c97112ea8.zip | |
implement a suggestion for a floating point number with a type suffix
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_typeck/src/check/expr.rs | 42 |
1 files changed, 33 insertions, 9 deletions
diff --git a/compiler/rustc_typeck/src/check/expr.rs b/compiler/rustc_typeck/src/check/expr.rs index a79257f0f5e..c3fc9776835 100644 --- a/compiler/rustc_typeck/src/check/expr.rs +++ b/compiler/rustc_typeck/src/check/expr.rs @@ -48,7 +48,7 @@ use rustc_middle::ty::{self, AdtKind, DefIdTree, Ty, TypeVisitable}; use rustc_session::parse::feature_err; use rustc_span::hygiene::DesugaringKind; use rustc_span::lev_distance::find_best_match_for_name; -use rustc_span::source_map::Span; +use rustc_span::source_map::{Span, Spanned}; use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{BytePos, Pos}; use rustc_target::spec::abi::Abi::RustIntrinsic; @@ -2170,14 +2170,38 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { E0610, "`{expr_t}` is a primitive type and therefore doesn't have fields", ); - if expr_t.is_integral() - && (field_name - .strip_prefix('e') - .or_else(|| field_name.strip_prefix('E')) - .map(|prefix| prefix.chars().all(|c| c.is_numeric())) - .unwrap_or_default() - || field.name == sym::f32 - || field.name == sym::f64) + let is_valid_suffix = |field: String| { + if field == "f32" || field == "f64" { + return true; + } + let mut chars = field.chars().peekable(); + match chars.peek() { + Some('e') | Some('E') => { + chars.next(); + if let Some(c) = chars.peek() + && !c.is_numeric() && *c != '-' && *c != '+' + { + return false; + } + while let Some(c) = chars.peek() { + if !c.is_numeric() { + break; + } + chars.next(); + } + } + _ => (), + } + let suffix = chars.collect::<String>(); + suffix.is_empty() || suffix == "f32" || suffix == "f64" + }; + if let ty::Infer(ty::IntVar(_)) = expr_t.kind() + && let ExprKind::Lit(Spanned { + node: ast::LitKind::Int(_, ast::LitIntType::Unsuffixed), + .. + }) = base.kind + && !base.span.from_expansion() + && is_valid_suffix(field_name) { err.span_suggestion_verbose( field.span.shrink_to_lo(), |
