Skip to content

Rollup of 5 pull requests #98925

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

Merged
merged 65 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
b96d1e4
change ptr::swap methods to do untyped copies
RalfJung Jun 3, 2022
cb7cd97
promise that ptr::copy and ptr::swap are doing untyped copies
RalfJung Jun 3, 2022
7d2eba6
middle: translation in `LintDiagnosticBuilder`
davidtwco Jun 27, 2022
0f4c4c5
lint: port array-into-iter diagnostics
davidtwco Jun 27, 2022
2a69640
lint: port enum intrinsics diagnostics
davidtwco Jun 27, 2022
a0624eb
lint: port expectation diagnostics
davidtwco Jun 27, 2022
fd57269
lint: port hidden unicode codepoints diagnostics
davidtwco Jun 27, 2022
e88916c
lint: port default hash types diagnostics
davidtwco Jun 27, 2022
8139542
lint: port query instability diagnostics
davidtwco Jun 27, 2022
0996a7a
lint: port ty diagnostics
davidtwco Jun 27, 2022
1c3a3e0
lint: port impl `LintPass` by hand diagnostics
davidtwco Jun 27, 2022
674ac60
lint: port non-existant doc keyword diagnostics
davidtwco Jun 27, 2022
4f35c79
lint: port translation migration diagnostics
davidtwco Jun 27, 2022
c29e05e
lint: port `CString` ptr diagnostics
davidtwco Jun 27, 2022
48e4bf1
lint: port non-ascii-idents diagnostics
davidtwco Jun 27, 2022
7ee4aa7
lint: port non-fmt-panic diagnostics
davidtwco Jun 27, 2022
096a69d
lint: port non-standard style diagnostics
davidtwco Jun 27, 2022
855f237
lint: port no-op method call diagnostics
davidtwco Jun 27, 2022
37588d6
lint: port pass-by-value diagnostics
davidtwco Jun 27, 2022
8e83656
lint: port redundant semicolons diagnostics
davidtwco Jun 27, 2022
7ef610c
lint: port drop trait/glue diagnostics
davidtwco Jun 27, 2022
7a9bef4
lint: port overflowing literals diagnostics
davidtwco Jun 27, 2022
e5f2e0e
lint: port improper ctypes diagnostics
davidtwco Jun 28, 2022
14c3016
lint: port variant size difference diagnostics
davidtwco Jun 28, 2022
0602729
lint: port atomic ordering diagnostics
davidtwco Jun 28, 2022
1999a4c
lint: port unused diagnostics
davidtwco Jun 28, 2022
2829f51
lint: port path statement diagnostics
davidtwco Jun 28, 2022
fc4f8d9
lint: port unused delimiter diagnostics
davidtwco Jun 28, 2022
e248338
lint: port unused import braces diagnostics
davidtwco Jun 28, 2022
4f7b10f
lint: port unused allocation diagnostics
davidtwco Jun 28, 2022
588977b
lint: port while true diagnostics
davidtwco Jun 28, 2022
d433c9a
lint: port box pointers diagnostics
davidtwco Jun 28, 2022
4c63a21
lint: port non-shorthand pattern diagnostics
davidtwco Jun 28, 2022
7dffd14
lint: port unsafe diagnostics
davidtwco Jun 28, 2022
82bd2c2
lint: port missing documentation diagnostics
davidtwco Jun 28, 2022
28655bc
lint: port missing copy impl diagnostics
davidtwco Jun 28, 2022
284ec37
lint: port missing debug impl diagnostics
davidtwco Jun 28, 2022
18a48c1
lint: port anonymous parameter diagnostics
davidtwco Jun 28, 2022
e151d66
lint: port deprecated attr diagnostics
davidtwco Jun 28, 2022
a13b70e
lint: port unused doc comment diagnostics
davidtwco Jun 28, 2022
dbdbdb6
lint: port no-mangle diagnostics
davidtwco Jun 28, 2022
d071f50
lint: port mutable transmutes diagnostic
davidtwco Jun 28, 2022
23ee3e0
lint: port unstable feature diagnostics
davidtwco Jun 28, 2022
dbced10
lint: port unreachable `pub` diagnostic
davidtwco Jun 28, 2022
01a64af
lint: port type alias bounds diagnostics
davidtwco Jun 28, 2022
3c9bda5
lint: port trivial bounds diagnostics
davidtwco Jun 28, 2022
3a498a7
lint: port `...` range pattern diagnostics
davidtwco Jun 28, 2022
10f2d3f
lint: port test items diagnostics
davidtwco Jun 28, 2022
1067641
lint: port keyword idents diagnostics
davidtwco Jun 28, 2022
acea23e
lint: port explicit outlives diagnostics
davidtwco Jun 28, 2022
bd8fe82
lint: port incomplete features diagnostics
davidtwco Jun 28, 2022
157cbbc
lint: add todo for invalid value diagnostics
davidtwco Jun 28, 2022
2e563a4
lint: port clashing extern diagnostics
davidtwco Jun 28, 2022
5524ca1
lint: port deref nullptr diagnostics
davidtwco Jun 28, 2022
fedd4c6
lint: port asm labels diagnostics
davidtwco Jun 28, 2022
9ff6c77
tests: avoid inadvertent diffs in diag derive test
davidtwco Jun 29, 2022
83f2288
Improve click behavior of the source code mobile full-screen "sidebar"
notriddle Jul 1, 2022
6e2c49f
rustdoc: add gui test case ensuring source sidebar doesn't spontaneou…
notriddle Jul 2, 2022
ce9e834
Remove FIXME from rustdoc intra-doc test
GuillaumeGomez Jul 3, 2022
170b173
:arrow_up: rust-analyzer
lnicola Jul 5, 2022
8fa1ed8
Rollup merge of #97712 - RalfJung:untyped, r=scottmcm
Dylan-DPC Jul 5, 2022
291df97
Rollup merge of #98624 - davidtwco:translation-on-lints, r=compiler-e…
Dylan-DPC Jul 5, 2022
c2613a5
Rollup merge of #98776 - notriddle:notriddle/mobile-sidebar-auto-clos…
Dylan-DPC Jul 5, 2022
6a61e18
Rollup merge of #98856 - GuillaumeGomez:rustdoc-test-rm-fixme, r=Dyla…
Dylan-DPC Jul 5, 2022
1036a38
Rollup merge of #98913 - lnicola:rust-analyzer-2022-07-05, r=lnicola
Dylan-DPC Jul 5, 2022
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
Prev Previous commit
Next Next commit
lint: port overflowing literals diagnostics
Signed-off-by: David Wood <[email protected]>
  • Loading branch information
davidtwco committed Jun 30, 2022
commit 7a9bef4d83a091be25fdf0f1beaade66eec0cd92
25 changes: 25 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/lint.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,28 @@ lint-drop-trait-constraints =

lint-drop-glue =
types that do not implement `Drop` can still have drop glue, consider instead using `{$needs_drop}` to detect whether a type is trivially dropped

lint-range-endpoint-out-of-range = range endpoint is out of range for `{$ty}`
.suggestion = use an inclusive range instead

lint-overflowing-bin-hex = literal out of range for `{$ty}`
.negative-note = the literal `{$lit}` (decimal `{$dec}`) does not fit into the type `{$ty}`
.negative-becomes-note = and the value `-{$lit}` will become `{$actually}{$ty}`
.positive-note = the literal `{$lit}` (decimal `{$dec}`) does not fit into the type `{$ty}` and will become `{$actually}{$ty}`
.suggestion = consider using the type `{$suggestion_ty}` instead
.help = consider using the type `{$suggestion_ty}` instead

lint-overflowing-int = literal out of range for `{$ty}`
.note = the literal `{$lit}` does not fit into the type `{$ty}` whose range is `{$min}..={$max}`
.help = consider using the type `{$suggestion_ty}` instead

lint-only-cast-u8-to-char = only `u8` can be cast into `char`
.suggestion = use a `char` literal instead

lint-overflowing-uint = literal out of range for `{$ty}`
.note = the literal `{$lit}` does not fit into the type `{$ty}` whose range is `{$min}..={$max}`

lint-overflowing-literal = literal out of range for `{$ty}`
.note = the literal `{$lit}` does not fit into the type `{$ty}` and will be converted to `{$ty}::INFINITY`

lint-unused-comparisons = comparison is useless due to type limits
12 changes: 12 additions & 0 deletions compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ impl IntoDiagnosticArg for bool {
}
}

impl IntoDiagnosticArg for i128 {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
}
}

impl IntoDiagnosticArg for u128 {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
}
}

impl IntoDiagnosticArg for String {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::Owned(self))
Expand Down
83 changes: 42 additions & 41 deletions compiler/rustc_lint/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{LateContext, LateLintPass, LintContext};
use rustc_ast as ast;
use rustc_attr as attr;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::Applicability;
use rustc_errors::{fluent, Applicability};
use rustc_hir as hir;
use rustc_hir::{is_range_literal, Expr, ExprKind, Node};
use rustc_middle::ty::layout::{IntegerExt, LayoutOf, SizeSkeleton};
Expand Down Expand Up @@ -139,7 +139,8 @@ fn lint_overflowing_range_endpoint<'tcx>(
// overflowing and only by 1.
if eps[1].expr.hir_id == expr.hir_id && lit_val - 1 == max {
cx.struct_span_lint(OVERFLOWING_LITERALS, parent_expr.span, |lint| {
let mut err = lint.build(&format!("range endpoint is out of range for `{}`", ty));
let mut err = lint.build(fluent::lint::range_endpoint_out_of_range);
err.set_arg("ty", ty);
if let Ok(start) = cx.sess().source_map().span_to_snippet(eps[0].span) {
use ast::{LitIntType, LitKind};
// We need to preserve the literal's suffix,
Expand All @@ -153,7 +154,7 @@ fn lint_overflowing_range_endpoint<'tcx>(
let suggestion = format!("{}..={}{}", start, lit_val - 1, suffix);
err.span_suggestion(
parent_expr.span,
"use an inclusive range instead",
fluent::lint::suggestion,
suggestion,
Applicability::MachineApplicable,
);
Expand Down Expand Up @@ -229,38 +230,35 @@ fn report_bin_hex_error(
(t.name_str(), actually.to_string())
}
};
let mut err = lint.build(&format!("literal out of range for `{}`", t));
let mut err = lint.build(fluent::lint::overflowing_bin_hex);
if negative {
// If the value is negative,
// emits a note about the value itself, apart from the literal.
err.note(&format!(
"the literal `{}` (decimal `{}`) does not fit into \
the type `{}`",
repr_str, val, t
));
err.note(&format!("and the value `-{}` will become `{}{}`", repr_str, actually, t));
err.note(fluent::lint::negative_note);
err.note(fluent::lint::negative_becomes_note);
} else {
err.note(&format!(
"the literal `{}` (decimal `{}`) does not fit into \
the type `{}` and will become `{}{}`",
repr_str, val, t, actually, t
));
err.note(fluent::lint::positive_note);
}
if let Some(sugg_ty) =
get_type_suggestion(cx.typeck_results().node_type(expr.hir_id), val, negative)
{
err.set_arg("suggestion_ty", sugg_ty);
if let Some(pos) = repr_str.chars().position(|c| c == 'i' || c == 'u') {
let (sans_suffix, _) = repr_str.split_at(pos);
err.span_suggestion(
expr.span,
&format!("consider using the type `{}` instead", sugg_ty),
fluent::lint::suggestion,
format!("{}{}", sans_suffix, sugg_ty),
Applicability::MachineApplicable,
);
} else {
err.help(&format!("consider using the type `{}` instead", sugg_ty));
err.help(fluent::lint::help);
}
}
err.set_arg("ty", t);
err.set_arg("lit", repr_str);
err.set_arg("dec", val);
err.set_arg("actually", actually);
err.emit();
});
}
Expand Down Expand Up @@ -353,21 +351,23 @@ fn lint_int_literal<'tcx>(
}

cx.struct_span_lint(OVERFLOWING_LITERALS, e.span, |lint| {
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 `{}..={}`",
let mut err = lint.build(fluent::lint::overflowing_int);
err.set_arg("ty", t.name_str());
err.set_arg(
"lit",
cx.sess()
.source_map()
.span_to_snippet(lit.span)
.expect("must get snippet from literal"),
t.name_str(),
min,
max,
));
);
err.set_arg("min", min);
err.set_arg("max", max);
err.note(fluent::lint::note);
if let Some(sugg_ty) =
get_type_suggestion(cx.typeck_results().node_type(e.hir_id), v, negative)
{
err.help(&format!("consider using the type `{}` instead", sugg_ty));
err.set_arg("suggestion_ty", sugg_ty);
err.help(fluent::lint::help);
}
err.emit();
});
Expand Down Expand Up @@ -395,10 +395,10 @@ fn lint_uint_literal<'tcx>(
hir::ExprKind::Cast(..) => {
if let ty::Char = cx.typeck_results().expr_ty(par_e).kind() {
cx.struct_span_lint(OVERFLOWING_LITERALS, par_e.span, |lint| {
lint.build("only `u8` can be cast into `char`")
lint.build(fluent::lint::only_cast_u8_to_char)
.span_suggestion(
par_e.span,
"use a `char` literal instead",
fluent::lint::suggestion,
format!("'\\u{{{:X}}}'", lit_val),
Applicability::MachineApplicable,
)
Expand Down Expand Up @@ -429,17 +429,18 @@ fn lint_uint_literal<'tcx>(
return;
}
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 `{}..={}`",
lint.build(fluent::lint::overflowing_uint)
.set_arg("ty", t.name_str())
.set_arg(
"lit",
cx.sess()
.source_map()
.span_to_snippet(lit.span)
.expect("must get snippet from literal"),
t.name_str(),
min,
max,
))
)
.set_arg("min", min)
.set_arg("max", max)
.note(fluent::lint::note)
.emit();
});
}
Expand Down Expand Up @@ -471,16 +472,16 @@ fn lint_literal<'tcx>(
};
if is_infinite == Ok(true) {
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 `{}` and will be converted to `{}::INFINITY`",
lint.build(fluent::lint::overflowing_literal)
.set_arg("ty", t.name_str())
.set_arg(
"lit",
cx.sess()
.source_map()
.span_to_snippet(lit.span)
.expect("must get snippet from literal"),
t.name_str(),
t.name_str(),
))
)
.note(fluent::lint::note)
.emit();
});
}
Expand All @@ -501,7 +502,7 @@ impl<'tcx> LateLintPass<'tcx> for TypeLimits {
hir::ExprKind::Binary(binop, ref l, ref r) => {
if is_comparison(binop) && !check_limits(cx, binop, &l, &r) {
cx.struct_span_lint(UNUSED_COMPARISONS, e.span, |lint| {
lint.build("comparison is useless due to type limits").emit();
lint.build(fluent::lint::unused_comparisons).emit();
});
}
}
Expand Down