Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'consider using' message to overflowing_literals #79981

Merged
merged 2 commits into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions compiler/rustc_lint/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,18 +331,23 @@ fn lint_int_literal<'tcx>(
}

cx.struct_span_lint(OVERFLOWING_LITERALS, e.span, |lint| {
lint.build(&format!("literal out of range for `{}`", t.name_str()))
.note(&format!(
"the literal `{}` does not fit into the type `{}` whose range is `{}..={}`",
cx.sess()
.source_map()
.span_to_snippet(lit.span)
.expect("must get snippet from literal"),
t.name_str(),
min,
max,
))
.emit();
let mut err = lint.build(&format!("literal out of range for `{}`", t.name_str()));
err.note(&format!(
"the literal `{}` does not fit into the type `{}` whose range is `{}..={}`",
cx.sess()
.source_map()
.span_to_snippet(lit.span)
.expect("must get snippet from literal"),
t.name_str(),
min,
max,
));
if let Some(sugg_ty) =
get_type_suggestion(&cx.typeck_results().node_type(e.hir_id), v, negative)
{
err.help(&format!("consider using `{}` instead", sugg_ty));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"using i8" sounds weird to me, maybe something like

Suggested change
err.help(&format!("consider using `{}` instead", sugg_ty));
err.help(&format!("consider changing its type to `{}`", sugg_ty));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just copied-and-pasted this from the binary/hexadecimal error. If we change this one, we should probably change the other one too.

Copy link
Contributor

@lcnr lcnr Dec 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, yeah I would prefer us to change this as well. Do you have a preference here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about

Suggested change
err.help(&format!("consider using `{}` instead", sugg_ty));
err.help(&format!("consider using the `{}` type instead", sugg_ty));

or something like that?

Copy link
Contributor

@lcnr lcnr Jan 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

somehow deleted my original message. edit: that seems fine to me ^^ though I would slightly adjust the wording here

Suggested change
err.help(&format!("consider using `{}` instead", sugg_ty));
err.help(&format!("consider using the type `{}` instead", sugg_ty));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are things like:

            ty::Char => FfiUnsafe {
                ty,
                reason: "the `char` type has no C equivalent".into(),
                help: Some("consider using `u32` or `libc::wchar_t` instead".into()),
            },

and

            ty::Str => FfiUnsafe {
                ty,
                reason: "string slices have no C equivalent".into(),
                help: Some("consider using `*const u8` and a length instead".into()),
            },

but those are for a different error so leaving them be.

}
err.emit();
});
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/enum/enum-discrim-too-small2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ note: the lint level is defined here
LL | #![deny(overflowing_literals)]
| ^^^^^^^^^^^^^^^^^^^^
= note: the literal `223` does not fit into the type `i8` whose range is `-128..=127`
= help: consider using `u8` instead

error: literal out of range for `i16`
--> $DIR/enum-discrim-too-small2.rs:15:12
Expand All @@ -18,6 +19,7 @@ LL | Ci16 = 55555,
| ^^^^^
|
= note: the literal `55555` does not fit into the type `i16` whose range is `-32768..=32767`
= help: consider using `u16` instead

error: literal out of range for `i32`
--> $DIR/enum-discrim-too-small2.rs:22:12
Expand All @@ -26,6 +28,7 @@ LL | Ci32 = 3_000_000_000,
| ^^^^^^^^^^^^^
|
= note: the literal `3_000_000_000` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
= help: consider using `u32` instead

error: literal out of range for `i64`
--> $DIR/enum-discrim-too-small2.rs:29:12
Expand All @@ -34,6 +37,7 @@ LL | Ci64 = 9223372036854775809,
| ^^^^^^^^^^^^^^^^^^^
|
= note: the literal `9223372036854775809` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
= help: consider using `u64` instead

error: aborting due to 4 previous errors

13 changes: 13 additions & 0 deletions src/test/ui/issues/issue-79744.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fn main() {
let elem = 6i8;
let e2 = 230;
//~^ ERROR literal out of range for `i8`
//~| HELP consider using `u8` instead

let mut vec = Vec::new();

vec.push(e2);
vec.push(elem);

println!("{:?}", vec);
}
12 changes: 12 additions & 0 deletions src/test/ui/issues/issue-79744.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error: literal out of range for `i8`
--> $DIR/issue-79744.rs:3:14
|
LL | let e2 = 230;
| ^^^
|
= note: `#[deny(overflowing_literals)]` on by default
= note: the literal `230` does not fit into the type `i8` whose range is `-128..=127`
= help: consider using `u8` instead

error: aborting due to previous error

1 change: 1 addition & 0 deletions src/test/ui/lint/lint-type-limits2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ note: the lint level is defined here
LL | #![warn(overflowing_literals)]
| ^^^^^^^^^^^^^^^^^^^^
= note: the literal `128` does not fit into the type `i8` whose range is `-128..=127`
= help: consider using `u8` instead

error: aborting due to previous error; 1 warning emitted

1 change: 1 addition & 0 deletions src/test/ui/lint/lint-type-limits3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ note: the lint level is defined here
LL | #![warn(overflowing_literals)]
| ^^^^^^^^^^^^^^^^^^^^
= note: the literal `200` does not fit into the type `i8` whose range is `-128..=127`
= help: consider using `u8` instead

error: aborting due to previous error; 1 warning emitted

16 changes: 16 additions & 0 deletions src/test/ui/lint/lint-type-overflow.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ LL | let x1: i8 = 128;
| ^^^
|
= note: the literal `128` does not fit into the type `i8` whose range is `-128..=127`
= help: consider using `u8` instead

error: literal out of range for `i8`
--> $DIR/lint-type-overflow.rs:18:19
Expand All @@ -34,6 +35,7 @@ LL | let x3: i8 = -129;
| ^^^
|
= note: the literal `129` does not fit into the type `i8` whose range is `-128..=127`
= help: consider using `i16` instead

error: literal out of range for `i8`
--> $DIR/lint-type-overflow.rs:19:19
Expand All @@ -42,6 +44,7 @@ LL | let x3: i8 = -(129);
| ^^^^^
|
= note: the literal `129` does not fit into the type `i8` whose range is `-128..=127`
= help: consider using `i16` instead

error: literal out of range for `i8`
--> $DIR/lint-type-overflow.rs:20:20
Expand All @@ -50,6 +53,7 @@ LL | let x3: i8 = -{129};
| ^^^
|
= note: the literal `129` does not fit into the type `i8` whose range is `-128..=127`
= help: consider using `u8` instead

error: literal out of range for `i8`
--> $DIR/lint-type-overflow.rs:22:10
Expand All @@ -58,6 +62,7 @@ LL | test(1000);
| ^^^^
|
= note: the literal `1000` does not fit into the type `i8` whose range is `-128..=127`
= help: consider using `i16` instead

error: literal out of range for `i8`
--> $DIR/lint-type-overflow.rs:24:13
Expand All @@ -66,6 +71,7 @@ LL | let x = 128_i8;
| ^^^^^^
|
= note: the literal `128_i8` does not fit into the type `i8` whose range is `-128..=127`
= help: consider using `u8` instead

error: literal out of range for `i8`
--> $DIR/lint-type-overflow.rs:28:14
Expand All @@ -74,6 +80,7 @@ LL | let x = -129_i8;
| ^^^^^^
|
= note: the literal `129_i8` does not fit into the type `i8` whose range is `-128..=127`
= help: consider using `i16` instead

error: literal out of range for `i32`
--> $DIR/lint-type-overflow.rs:32:18
Expand All @@ -82,6 +89,7 @@ LL | let x: i32 = 2147483648;
| ^^^^^^^^^^
|
= note: the literal `2147483648` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
= help: consider using `u32` instead

error: literal out of range for `i32`
--> $DIR/lint-type-overflow.rs:33:13
Expand All @@ -90,6 +98,7 @@ LL | let x = 2147483648_i32;
| ^^^^^^^^^^^^^^
|
= note: the literal `2147483648_i32` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
= help: consider using `u32` instead

error: literal out of range for `i32`
--> $DIR/lint-type-overflow.rs:36:19
Expand All @@ -98,6 +107,7 @@ LL | let x: i32 = -2147483649;
| ^^^^^^^^^^
|
= note: the literal `2147483649` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
= help: consider using `i64` instead

error: literal out of range for `i32`
--> $DIR/lint-type-overflow.rs:37:14
Expand All @@ -106,6 +116,7 @@ LL | let x = -2147483649_i32;
| ^^^^^^^^^^^^^^
|
= note: the literal `2147483649_i32` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
= help: consider using `i64` instead

error: literal out of range for `i32`
--> $DIR/lint-type-overflow.rs:38:13
Expand All @@ -114,6 +125,7 @@ LL | let x = 2147483648;
| ^^^^^^^^^^
|
= note: the literal `2147483648` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
= help: consider using `u32` instead

error: literal out of range for `i64`
--> $DIR/lint-type-overflow.rs:40:13
Expand All @@ -122,6 +134,7 @@ LL | let x = 9223372036854775808_i64;
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the literal `9223372036854775808_i64` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
= help: consider using `u64` instead

error: literal out of range for `i64`
--> $DIR/lint-type-overflow.rs:42:13
Expand All @@ -130,6 +143,7 @@ LL | let x = 18446744073709551615_i64;
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the literal `18446744073709551615_i64` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
= help: consider using `u64` instead

error: literal out of range for `i64`
--> $DIR/lint-type-overflow.rs:43:19
Expand All @@ -138,6 +152,7 @@ LL | let x: i64 = -9223372036854775809;
| ^^^^^^^^^^^^^^^^^^^
|
= note: the literal `9223372036854775809` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
= help: consider using `i128` instead

error: literal out of range for `i64`
--> $DIR/lint-type-overflow.rs:44:14
Expand All @@ -146,6 +161,7 @@ LL | let x = -9223372036854775809_i64;
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the literal `9223372036854775809_i64` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
= help: consider using `i128` instead

error: aborting due to 18 previous errors

1 change: 1 addition & 0 deletions src/test/ui/lint/lint-type-overflow2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ note: the lint level is defined here
LL | #![deny(overflowing_literals)]
| ^^^^^^^^^^^^^^^^^^^^
= note: the literal `128` does not fit into the type `i8` whose range is `-128..=127`
= help: consider using `u8` instead

error: literal out of range for `f32`
--> $DIR/lint-type-overflow2.rs:9:14
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/lint/type-overflow.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ note: the lint level is defined here
LL | #![warn(overflowing_literals)]
| ^^^^^^^^^^^^^^^^^^^^
= note: the literal `255i8` does not fit into the type `i8` whose range is `-128..=127`
= help: consider using `u8` instead

warning: literal out of range for i8
--> $DIR/type-overflow.rs:10:16
Expand Down