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

compiler: factor out OVERFLOWING_LITERALS impl #130646

Merged
merged 2 commits into from
Sep 21, 2024

Conversation

workingjubilee
Copy link
Member

This puts it into rustc_lint/src/types/literal.rs. It then uses the fact that it's easier to navigate the logic to identify something that can easily be factored out, as an instance of "why".

no functional changes should arise, just moves the lint impl details out
of a very crowded file with lots of different lints in it.
no need for a weird macro when a self-explanatory `match` will do.
@rustbot
Copy link
Collaborator

rustbot commented Sep 21, 2024

r? @michaelwoerister

rustbot has assigned @michaelwoerister.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 21, 2024
@compiler-errors
Copy link
Member

I confirmed that this is mostly just moving things around. Thanks!

r? compiler-errors @bors r+ rollup

@bors
Copy link
Contributor

bors commented Sep 21, 2024

📌 Commit 844edfe has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 21, 2024
Comment on lines -384 to -420
use ty::IntTy::*;
use ty::UintTy::*;
macro_rules! find_fit {
($ty:expr, $val:expr, $negative:expr,
$($type:ident => [$($utypes:expr),*] => [$($itypes:expr),*]),+) => {
{
let _neg = if negative { 1 } else { 0 };
match $ty {
$($type => {
$(if !negative && val <= uint_ty_range($utypes).1 {
return Some($utypes.name_str())
})*
$(if val <= int_ty_range($itypes).1 as u128 + _neg {
return Some($itypes.name_str())
})*
None
},)+
_ => None
}
}
}
}
match t.kind() {
ty::Int(i) => find_fit!(i, val, negative,
I8 => [U8] => [I16, I32, I64, I128],
I16 => [U16] => [I32, I64, I128],
I32 => [U32] => [I64, I128],
I64 => [U64] => [I128],
I128 => [U128] => []),
ty::Uint(u) => find_fit!(u, val, negative,
U8 => [U8, U16, U32, U64, U128] => [],
U16 => [U16, U32, U64, U128] => [],
U32 => [U32, U64, U128] => [],
U64 => [U64, U128] => [],
U128 => [U128] => []),
_ => None,
}
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 at least find this kind of macro-heavy stuff extremely hard to read or understand. And I find "locating knotty bits of code in a 2000-line file" somewhat challenging, too.

bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 21, 2024
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#129718 (add guarantee about remove_dir and remove_file error kinds)
 - rust-lang#130598 (Add recursion limit to FFI safety lint)
 - rust-lang#130642 (Pass the current cargo to `run-make` tests)
 - rust-lang#130644 (Only expect valtree consts in codegen)
 - rust-lang#130645 (Normalize consts in writeback when GCE is enabled)
 - rust-lang#130646 (compiler: factor out `OVERFLOWING_LITERALS` impl)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 5770ba8 into rust-lang:master Sep 21, 2024
6 checks passed
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Sep 21, 2024
Rollup merge of rust-lang#130646 - workingjubilee:literally-factorize-int-lint, r=compiler-errors

compiler: factor out `OVERFLOWING_LITERALS` impl

This puts it into `rustc_lint/src/types/literal.rs`. It then uses the fact that it's easier to navigate the logic to identify something that can easily be factored out, as an instance of "why".
@rustbot rustbot added this to the 1.83.0 milestone Sep 21, 2024
@workingjubilee workingjubilee deleted the literally-factorize-int-lint branch September 21, 2024 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants