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

Show better warning for trying to cast non-u8 scalar to char #44201

Closed
Manishearth opened this issue Aug 31, 2017 · 1 comment
Closed

Show better warning for trying to cast non-u8 scalar to char #44201

Manishearth opened this issue Aug 31, 2017 · 1 comment
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics

Comments

@Manishearth
Copy link
Member

Manishearth commented Aug 31, 2017

fn main() {
    const XYZ: char = 0xEF8888 as char;
    println!("{}", XYZ);
}

Will warn "literal is out of range for u8" because u8 is the only as char cast that is acceptable.

We should show a better warning and suggest the \u syntax instead.

@Manishearth Manishearth added the A-diagnostics Area: Messages for errors, warnings, and lints label Aug 31, 2017
@shepmaster shepmaster added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 1, 2017
@estebank
Copy link
Contributor

In the following code:

ty::TyUint(t) => {
let uint_type = if let ast::UintTy::Us = t {
cx.sess().target.usize_ty
} else {
t
};
let (min, max) = uint_ty_range(uint_type);
let lit_val: u128 = match lit.node {
// _v is u8, within range by definition
ast::LitKind::Byte(_v) => return,
ast::LitKind::Int(v, _) => v,
_ => bug!(),
};
if lit_val < min || lit_val > max {
cx.span_lint(OVERFLOWING_LITERALS,
e.span,
&format!("literal out of range for {:?}", t));
}

check if e.hir_id parent expr is a cast to char and emit a help explaining that only u8 can be casted to char and a suggestion to write const XYZ: char = '\u{EF8888}'; instead.

@estebank estebank added E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. WG-diagnostics Working group: Diagnostics labels Dec 13, 2017
@GuillaumeGomez GuillaumeGomez self-assigned this Feb 6, 2018
kennytm added a commit to kennytm/rust that referenced this issue Feb 12, 2018
…sage, r=estebank

Show better warning for trying to cast non-u8 scalar to char

Fixes rust-lang#44201.
kennytm added a commit to kennytm/rust that referenced this issue Feb 13, 2018
…sage, r=estebank

Show better warning for trying to cast non-u8 scalar to char

Fixes rust-lang#44201.
kennytm added a commit to kennytm/rust that referenced this issue Feb 14, 2018
…sage, r=estebank

Show better warning for trying to cast non-u8 scalar to char

Fixes rust-lang#44201.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

No branches or pull requests

4 participants