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

Change some helps to suggestions #31053

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 15 additions & 8 deletions src/librustc_lint/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,19 @@ impl LateLintPass for TypeLimits {
if let hir::ExprLit(ref lit) = expr.node {
match lit.node {
ast::LitInt(_, ast::UnsignedIntLit(_)) => {
forbid_unsigned_negation(cx, e.span);
forbid_unsigned_negation(cx, e.span, lit.span);
},
ast::LitInt(_, ast::UnsuffixedIntLit(_)) => {
if let ty::TyUint(_) = cx.tcx.node_id_to_type(e.id).sty {
forbid_unsigned_negation(cx, e.span);
forbid_unsigned_negation(cx, e.span, lit.span);
}
},
_ => ()
}
} else {
let t = cx.tcx.node_id_to_type(expr.id);
if let ty::TyUint(_) = t.sty {
forbid_unsigned_negation(cx, e.span);
forbid_unsigned_negation(cx, e.span, expr.span);
}
}
// propagate negation, if the negation itself isn't negated
Expand Down Expand Up @@ -344,11 +344,18 @@ impl LateLintPass for TypeLimits {
}
}

fn forbid_unsigned_negation(cx: &LateContext, span: Span) {
cx.sess()
.struct_span_err_with_code(span, "unary negation of unsigned integer", "E0519")
.span_help(span, "use a cast or the `!` operator")
.emit();
fn forbid_unsigned_negation(cx: &LateContext, span: Span, lit_span: Span) {
let mut err = cx.sess()
.struct_span_err_with_code(span,
"unary negation of unsigned integer",
"E0519");
if let Ok(snip) = cx.sess().codemap().span_to_snippet(lit_span) {
err.span_suggestion(span, "try using a cast or the `!` operator:",
format!("!{}", snip));
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the cast way is the only one you can do generically. as -1isize as usize does not yield the same as !1usize. It would need to be !(x-1), which errors if x == 0

Copy link
Member Author

Choose a reason for hiding this comment

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

Huh. Adding a generic cast here is harder so I'll just fix up the help message.

} else {
err.span_help(span, "use a cast or the `!` operator");
}
err.emit();
}
}
}
Expand Down
49 changes: 20 additions & 29 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,12 +420,22 @@ fn resolve_struct_error<'b, 'a: 'b, 'tcx: 'a>(resolver: &'b Resolver<'a, 'tcx>,
name)
}
ResolutionError::StructVariantUsedAsFunction(path_name) => {
struct_span_err!(resolver.session,
span,
E0423,
"`{}` is the name of a struct or struct variant, but this expression \
uses it like a function name",
path_name)
let mut err = struct_span_err!(resolver.session,
span,
E0423,
"`{}` is the name of a struct or \
struct variant, but this expression \
uses it like a function name",
path_name);
if resolver.emit_errors {
let msg = format!("did you mean to write: `{} {{ /* fields */ }}`?",
path_name);
err.fileline_help(span, &msg);
} else {
let suggestion = format!("{} {{ /* fields */ }}", path_name);
err.span_suggestion(span, "did you mean to write", suggestion);
}
err
}
ResolutionError::SelfNotAvailableInStaticMethod => {
struct_span_err!(resolver.session,
Expand Down Expand Up @@ -3521,18 +3531,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
if let DefVariant(_, _, true) = path_res.base_def {
let path_name = path_names_to_string(path, 0);

let mut err = resolve_struct_error(self,
expr.span,
ResolutionError::StructVariantUsedAsFunction(&*path_name));
resolve_error(self, expr.span,
ResolutionError::StructVariantUsedAsFunction(&*path_name));

let msg = format!("did you mean to write: `{} {{ /* fields */ }}`?",
path_name);
if self.emit_errors {
err.fileline_help(expr.span, &msg);
} else {
err.span_help(expr.span, &msg);
}
err.emit();
self.record_def(expr.id, err_path_resolution());
} else {
// Write the result into the def map.
Expand Down Expand Up @@ -3562,18 +3563,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
self.record_def(expr.id, err_path_resolution());
match type_res.map(|r| r.base_def) {
Some(DefTy(struct_id, _)) if self.structs.contains_key(&struct_id) => {
let mut err = resolve_struct_error(self,
expr.span,
ResolutionError::StructVariantUsedAsFunction(&*path_name));

let msg = format!("did you mean to write: `{} {{ /* fields */ }}`?",
path_name);
if self.emit_errors {
err.fileline_help(expr.span, &msg);
} else {
err.span_help(expr.span, &msg);
}
err.emit();
resolve_error(self, expr.span,
ResolutionError::StructVariantUsedAsFunction(&*path_name));
}
_ => {
// Keep reporting some errors even if they're ignored above.
Expand Down
14 changes: 9 additions & 5 deletions src/test/compile-fail/feature-gate-negate-unsigned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,29 @@ impl std::ops::Neg for S {

const _MAX: usize = -1;
//~^ ERROR unary negation of unsigned integer
//~| HELP use a cast or the `!` operator
//~| HELP try using a cast or the `!` operator

fn main() {
let a = -1;
//~^ ERROR unary negation of unsigned integer
//~| HELP use a cast or the `!` operator
//~| HELP try using a cast or the `!` operator
//~| SUGGESTION let a = !1;
let _b : u8 = a; // for infering variable a to u8.

-a;
//~^ ERROR unary negation of unsigned integer
//~| HELP use a cast or the `!` operator
//~| HELP try using a cast or the `!` operator
//~| SUGGESTION !a

let _d = -1u8;
//~^ ERROR unary negation of unsigned integer
//~| HELP use a cast or the `!` operator
//~| HELP try using a cast or the `!` operator
//~| SUGGESTION let _d = !1u8;

for _ in -10..10u8 {}
//~^ ERROR unary negation of unsigned integer
//~| HELP use a cast or the `!` operator
//~| HELP try using a cast or the `!` operator
//~| SUGGESTION for _ in !10..10u8 {}

-S; // should not trigger the gate; issue 26840
}