Skip to content

Commit 42cf687

Browse files
committed
Use more accurate span for "specify numeric literal type" suggestion
1 parent bbe616a commit 42cf687

9 files changed

+49
-37
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ use rustc_middle::ty::IsSuggestable;
3333
use rustc_middle::ty::{self, GenericArgKind, Ty, TyCtxt, TypeVisitableExt};
3434
use rustc_span::def_id::DefIdSet;
3535
use rustc_span::symbol::{kw, sym, Ident};
36-
use rustc_span::{edit_distance, ErrorGuaranteed, ExpnKind, FileName, MacroKind, Span};
37-
use rustc_span::{Symbol, DUMMY_SP};
36+
use rustc_span::{
37+
edit_distance, BytePos, ErrorGuaranteed, ExpnKind, FileName, MacroKind, Span, Symbol, DUMMY_SP,
38+
};
3839
use rustc_trait_selection::error_reporting::traits::on_unimplemented::OnUnimplementedNote;
3940
use rustc_trait_selection::error_reporting::traits::on_unimplemented::TypeErrCtxtExt as _;
4041
use rustc_trait_selection::infer::InferCtxtExt;
@@ -2551,14 +2552,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
25512552

25522553
// If this is a floating point literal that ends with '.',
25532554
// get rid of it to stop this from becoming a member access.
2554-
let snippet = snippet.strip_suffix('.').unwrap_or(&snippet);
2555-
err.span_suggestion(
2556-
lit.span,
2555+
let mut span = lit.span.shrink_to_hi();
2556+
if snippet.ends_with('.') {
2557+
span = span.with_lo(span.lo() - BytePos(1));
2558+
}
2559+
err.span_suggestion_verbose(
2560+
span,
25572561
format!(
2558-
"you must specify a concrete type for this numeric value, \
2559-
like `{concrete_type}`"
2562+
"you must specify a concrete type for this numeric value, like \
2563+
`{concrete_type}`"
25602564
),
2561-
format!("{snippet}_{concrete_type}"),
2565+
format!("_{concrete_type}"),
25622566
Applicability::MaybeIncorrect,
25632567
);
25642568
}

tests/ui/cfg/cfg-method-receiver.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ LL | cbor_map! { #[cfg(test)] 4};
1616
= note: this error originates in the macro `cbor_map` (in Nightly builds, run with -Z macro-backtrace for more info)
1717
help: you must specify a concrete type for this numeric value, like `i32`
1818
|
19-
LL - cbor_map! { #[cfg(test)] 4};
20-
LL + cbor_map! { #[cfg(test)] 4_i32};
21-
|
19+
LL | cbor_map! { #[cfg(test)] 4_i32};
20+
| ++++
2221

2322
error: aborting due to 2 previous errors
2423

tests/ui/issues/issue-41652/issue-41652.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ LL | 3.f()
66
|
77
help: you must specify a concrete type for this numeric value, like `i32`
88
|
9-
LL - 3.f()
10-
LL + 3_i32.f()
11-
|
9+
LL | 3_i32.f()
10+
| ++++
1211

1312
error: aborting due to 1 previous error
1413

tests/ui/issues/issue-51874.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ LL | let a = (1.0).pow(1.0);
66
|
77
help: you must specify a concrete type for this numeric value, like `f32`
88
|
9-
LL - let a = (1.0).pow(1.0);
10-
LL + let a = (1.0_f32).pow(1.0);
11-
|
9+
LL | let a = (1.0_f32).pow(1.0);
10+
| ++++
1211

1312
error: aborting due to 1 previous error
1413

tests/ui/macros/macro-backtrace-invalid-internals.stderr

+4-6
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ LL | real_method_stmt!();
4343
= note: this error originates in the macro `real_method_stmt` (in Nightly builds, run with -Z macro-backtrace for more info)
4444
help: you must specify a concrete type for this numeric value, like `f32`
4545
|
46-
LL - 2.0.neg()
47-
LL + 2.0_f32.neg()
48-
|
46+
LL | 2.0_f32.neg()
47+
| ++++
4948

5049
error[E0599]: no method named `fake` found for type `{integer}` in the current scope
5150
--> $DIR/macro-backtrace-invalid-internals.rs:23:13
@@ -92,9 +91,8 @@ LL | let _ = real_method_expr!();
9291
= note: this error originates in the macro `real_method_expr` (in Nightly builds, run with -Z macro-backtrace for more info)
9392
help: you must specify a concrete type for this numeric value, like `f32`
9493
|
95-
LL - 2.0.neg()
96-
LL + 2.0_f32.neg()
97-
|
94+
LL | 2.0_f32.neg()
95+
| ++++
9896

9997
error: aborting due to 8 previous errors
10098

tests/ui/methods/issues/issue-90315.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,8 @@ LL | let _res: i32 = ..6.take(2).sum();
181181
|
182182
help: you must specify a concrete type for this numeric value, like `i32`
183183
|
184-
LL - let _res: i32 = ..6.take(2).sum();
185-
LL + let _res: i32 = ..6_i32.take(2).sum();
186-
|
184+
LL | let _res: i32 = ..6_i32.take(2).sum();
185+
| ++++
187186

188187
error: aborting due to 18 previous errors
189188

tests/ui/methods/method-on-ambiguous-numeric-type.rs

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ fn main() {
1313
let x = 2.0.neg();
1414
//~^ ERROR can't call method `neg` on ambiguous numeric type `{float}`
1515

16+
let y = 2.;
17+
let x = y.neg();
18+
//~^ ERROR can't call method `neg` on ambiguous numeric type `{float}`
19+
println!("{:?}", x);
20+
1621
let y = 2.0;
1722
let x = y.neg();
1823
//~^ ERROR can't call method `neg` on ambiguous numeric type `{float}`

tests/ui/methods/method-on-ambiguous-numeric-type.stderr

+18-8
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ LL | let x = 2.0.neg();
66
|
77
help: you must specify a concrete type for this numeric value, like `f32`
88
|
9-
LL - let x = 2.0.neg();
10-
LL + let x = 2.0_f32.neg();
11-
|
9+
LL | let x = 2.0_f32.neg();
10+
| ++++
1211

1312
error[E0689]: can't call method `neg` on ambiguous numeric type `{float}`
1413
--> $DIR/method-on-ambiguous-numeric-type.rs:17:15
@@ -18,19 +17,30 @@ LL | let x = y.neg();
1817
|
1918
help: you must specify a type for this binding, like `f32`
2019
|
20+
LL | let y: f32 = 2.;
21+
| +++++
22+
23+
error[E0689]: can't call method `neg` on ambiguous numeric type `{float}`
24+
--> $DIR/method-on-ambiguous-numeric-type.rs:22:15
25+
|
26+
LL | let x = y.neg();
27+
| ^^^
28+
|
29+
help: you must specify a type for this binding, like `f32`
30+
|
2131
LL | let y: f32 = 2.0;
2232
| +++++
2333

2434
error[E0689]: can't call method `pow` on ambiguous numeric type `{integer}`
25-
--> $DIR/method-on-ambiguous-numeric-type.rs:22:26
35+
--> $DIR/method-on-ambiguous-numeric-type.rs:27:26
2636
|
2737
LL | for i in 0..100 {
2838
| - you must specify a type for this binding, like `i32`
2939
LL | println!("{}", i.pow(2));
3040
| ^^^
3141

3242
error[E0689]: can't call method `pow` on ambiguous numeric type `{integer}`
33-
--> $DIR/method-on-ambiguous-numeric-type.rs:27:15
43+
--> $DIR/method-on-ambiguous-numeric-type.rs:32:15
3444
|
3545
LL | local_bar.pow(2);
3646
| ^^^
@@ -41,7 +51,7 @@ LL | ($ident:ident) => { let $ident: i32 = 42; }
4151
| +++++
4252

4353
error[E0689]: can't call method `pow` on ambiguous numeric type `{integer}`
44-
--> $DIR/method-on-ambiguous-numeric-type.rs:31:18
54+
--> $DIR/method-on-ambiguous-numeric-type.rs:36:18
4555
|
4656
LL | local_bar_tt.pow(2);
4757
| ^^^
@@ -52,7 +62,7 @@ LL | local_mac_tt!(local_bar_tt: i32);
5262
| +++++
5363

5464
error[E0689]: can't call method `pow` on ambiguous numeric type `{integer}`
55-
--> $DIR/method-on-ambiguous-numeric-type.rs:37:9
65+
--> $DIR/method-on-ambiguous-numeric-type.rs:42:9
5666
|
5767
LL | bar.pow(2);
5868
| ^^^
@@ -63,6 +73,6 @@ help: you must specify a type for this binding, like `i32`
6373
LL | ($ident:ident) => { let $ident: i32 = 42; }
6474
| +++++
6575

66-
error: aborting due to 6 previous errors
76+
error: aborting due to 7 previous errors
6777

6878
For more information about this error, try `rustc --explain E0689`.

tests/ui/typeck/issue-29181.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ LL | let _ = |x: f64| x * 2.0.exp();
1212
|
1313
help: you must specify a concrete type for this numeric value, like `f32`
1414
|
15-
LL - let _ = |x: f64| x * 2.0.exp();
16-
LL + let _ = |x: f64| x * 2.0_f32.exp();
17-
|
15+
LL | let _ = |x: f64| x * 2.0_f32.exp();
16+
| ++++
1817

1918
error: aborting due to 2 previous errors
2019

0 commit comments

Comments
 (0)