@@ -48,7 +48,7 @@ use rustc_middle::ty::{self, AdtKind, DefIdTree, Ty, TypeVisitable};
48
48
use rustc_session:: parse:: feature_err;
49
49
use rustc_span:: hygiene:: DesugaringKind ;
50
50
use rustc_span:: lev_distance:: find_best_match_for_name;
51
- use rustc_span:: source_map:: Span ;
51
+ use rustc_span:: source_map:: { Span , Spanned } ;
52
52
use rustc_span:: symbol:: { kw, sym, Ident , Symbol } ;
53
53
use rustc_span:: { BytePos , Pos } ;
54
54
use rustc_target:: spec:: abi:: Abi :: RustIntrinsic ;
@@ -2162,14 +2162,55 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2162
2162
} else if !expr_t. is_primitive_ty ( ) {
2163
2163
self . ban_nonexisting_field ( field, base, expr, expr_t) ;
2164
2164
} else {
2165
- type_error_struct ! (
2165
+ let field_name = field. to_string ( ) ;
2166
+ let mut err = type_error_struct ! (
2166
2167
self . tcx( ) . sess,
2167
2168
field. span,
2168
2169
expr_t,
2169
2170
E0610 ,
2170
2171
"`{expr_t}` is a primitive type and therefore doesn't have fields" ,
2171
- )
2172
- . emit ( ) ;
2172
+ ) ;
2173
+ let is_valid_suffix = |field : String | {
2174
+ if field == "f32" || field == "f64" {
2175
+ return true ;
2176
+ }
2177
+ let mut chars = field. chars ( ) . peekable ( ) ;
2178
+ match chars. peek ( ) {
2179
+ Some ( 'e' ) | Some ( 'E' ) => {
2180
+ chars. next ( ) ;
2181
+ if let Some ( c) = chars. peek ( )
2182
+ && !c. is_numeric ( ) && * c != '-' && * c != '+'
2183
+ {
2184
+ return false ;
2185
+ }
2186
+ while let Some ( c) = chars. peek ( ) {
2187
+ if !c. is_numeric ( ) {
2188
+ break ;
2189
+ }
2190
+ chars. next ( ) ;
2191
+ }
2192
+ }
2193
+ _ => ( ) ,
2194
+ }
2195
+ let suffix = chars. collect :: < String > ( ) ;
2196
+ suffix. is_empty ( ) || suffix == "f32" || suffix == "f64"
2197
+ } ;
2198
+ if let ty:: Infer ( ty:: IntVar ( _) ) = expr_t. kind ( )
2199
+ && let ExprKind :: Lit ( Spanned {
2200
+ node : ast:: LitKind :: Int ( _, ast:: LitIntType :: Unsuffixed ) ,
2201
+ ..
2202
+ } ) = base. kind
2203
+ && !base. span . from_expansion ( )
2204
+ && is_valid_suffix ( field_name)
2205
+ {
2206
+ err. span_suggestion_verbose (
2207
+ field. span . shrink_to_lo ( ) ,
2208
+ "If the number is meant to be a floating point number, consider adding a `0` after the period" ,
2209
+ '0' ,
2210
+ Applicability :: MaybeIncorrect ,
2211
+ ) ;
2212
+ }
2213
+ err. emit ( ) ;
2173
2214
}
2174
2215
2175
2216
self . tcx ( ) . ty_error ( )
0 commit comments