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

Do not inline integer literals which are out of range in format_args! #116633

Closed
wants to merge 1 commit into from

Conversation

agluszak
Copy link

@agluszak agluszak commented Oct 11, 2023

Inlining integers (even those out of range) was introduced in #106824.

Closes #116631.

Closes #115423.

@rustbot
Copy link
Collaborator

rustbot commented Oct 11, 2023

r? @compiler-errors

(rustbot has picked a reviewer for you, use r? to override)

@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 Oct 11, 2023
@agluszak
Copy link
Author

Technically that's a breaking change...

@compiler-errors
Copy link
Member

Hm yeah. Nominating for T-lang, impl looks ok but I didn't look at it too long -- will do after T-lang confirms we should re-break this.

In the mean time, I'll do a crater run.

@bors try

@compiler-errors compiler-errors added the I-lang-nominated Nominated for discussion during a lang team meeting. label Oct 11, 2023
@bors
Copy link
Contributor

bors commented Oct 11, 2023

⌛ Trying commit b0ae218 with merge b67bddc...

bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 11, 2023
Do not inline integer linterals which are out of range in format_args!

Inlining integers (even those out of range) was introduced in rust-lang#106824.

Closes rust-lang#116631.
@bors
Copy link
Contributor

bors commented Oct 11, 2023

☀️ Try build successful - checks-actions
Build commit: b67bddc (b67bddc71c8ce18bca43818e26d6290f72650a92)

@compiler-errors
Copy link
Member

@craterbot check

@craterbot
Copy link
Collaborator

👌 Experiment pr-116633 created and queued.
🤖 Automatically detected try build b67bddc
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 11, 2023
@craterbot
Copy link
Collaborator

🚧 Experiment pr-116633 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

LitIntType::Signed(IntTy::I32) => i32::MAX as u128,
LitIntType::Signed(IntTy::I64) => i64::MAX as u128,
LitIntType::Signed(IntTy::I128) => i128::MAX as u128,
LitIntType::Signed(IntTy::Isize) => isize::MAX as u128,
Copy link
Member

Choose a reason for hiding this comment

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

This uses the host's isize, but should be using use the target's isize. Same for usize below.

Copy link
Author

Choose a reason for hiding this comment

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

Fair. What should I do to obtain these values for the target?

Copy link
Member

@lukas-code lukas-code Oct 11, 2023

Choose a reason for hiding this comment

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

You can get size of isize and usize from tcx.data_layout.pointer_size. That will return a Size, which has the signed_int_max and unsigned_int_max methods that seem useful. It looks like the nearest tcx is the one in the LoweringContext, which you get as self from here:

pub(crate) fn lower_format_args(&mut self, sp: Span, fmt: &FormatArgs) -> hir::ExprKind<'hir> {

LitIntType::Unsigned(UintTy::U64) => u64::MAX as u128,
LitIntType::Unsigned(UintTy::U128) => u128::MAX,
LitIntType::Unsigned(UintTy::Usize) => usize::MAX as u128,
LitIntType::Unsuffixed => u128::MAX,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
LitIntType::Unsuffixed => u128::MAX,
LitIntType::Unsuffixed => i32::MAX,

Unsuffixed and unconstrained integer literals should have type i32, even if they overflow.

See the old behavior before inlining: https://rust.godbolt.org/z/PMG7ac4d4

@@ -0,0 +1,3 @@
fn main() {
format_args!("{}\n", 0xffff_ffff_u8); //~ ERROR literal out of range for `u8`
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this have a test for each of the different integer types?

Copy link
Member

Choose a reason for hiding this comment

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

At the very least it should have a test case that covers #115423 (which I don't interpet is about being out of range, but rather about not respecting the type of the literal).

@asquared31415
Copy link
Contributor

asquared31415 commented Oct 11, 2023

According to the linked PR, it's meant to be gated behind -Zflatten-format-args=yes, is that gating broken somehow?

Edit: ah that flag seems to have been stabilized to default to on, I didn't see that

@agluszak agluszak changed the title Do not inline integer linterals which are out of range in format_args! Do not inline integer literals which are out of range in format_args! Oct 11, 2023
@craterbot
Copy link
Collaborator

🎉 Experiment pr-116633 is completed!
📊 13 regressed and 0 fixed (376984 total)
📰 Open the full report.

⚠️ If you notice any spurious failure please add them to the blacklist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Oct 13, 2023
@scottmcm
Copy link
Member

We discussed this in the @rust-lang/lang triage meeting today.

Everyone agreed that this was "just" a bug, and should be fixed. Printing a token that's explicitly marked an i8 and getting a result outside the range of an i8 is clearly wrong.

Ideally this would trigger the overflowing_literals lint same as using a token like this as an expression in other places.

@traviscross
Copy link
Contributor

@rustbot labels -I-lang-nominated

@rustbot rustbot removed the I-lang-nominated Nominated for discussion during a lang team meeting. label Oct 24, 2023
@compiler-errors
Copy link
Member

Awaiting feedback above

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 25, 2023
@agluszak
Copy link
Author

I'll fix it on Saturday or Sunday

@JohnCSimon
Copy link
Member

@agluszak

ping from triage - can you post your status on this PR? This PR has not received an update in a few months. Thank you!

FYI: when a PR is ready for review, send a message containing
@rustbot ready to switch to S-waiting-on-review so the PR is in the reviewer's backlog.

@Dylan-DPC
Copy link
Member

Closing this as inactive. Feel free to reöpen this pr or create a new pr if you get the time to work on this. Thanks

@Dylan-DPC Dylan-DPC closed this Apr 10, 2024
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Apr 18, 2024
Don't inline integer literals when they overflow - new attempt

Basically rust-lang#116633 but I implemented the suggested changes.
Fixes rust-lang#115423. Fixes rust-lang#116631.

This is my first contribution to this repo so please let me know if I'm supposed to change something :)
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Apr 18, 2024
Don't inline integer literals when they overflow - new attempt

Basically rust-lang#116633 but I implemented the suggested changes.
Fixes rust-lang#115423. Fixes rust-lang#116631.

This is my first contribution to this repo so please let me know if I'm supposed to change something :)
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Apr 19, 2024
Don't inline integer literals when they overflow - new attempt

Basically rust-lang#116633 but I implemented the suggested changes.
Fixes rust-lang#115423. Fixes rust-lang#116631.

This is my first contribution to this repo so please let me know if I'm supposed to change something :)
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 19, 2024
Rollup merge of rust-lang#123935 - tstsrt:fix-115423, r=oli-obk

Don't inline integer literals when they overflow - new attempt

Basically rust-lang#116633 but I implemented the suggested changes.
Fixes rust-lang#115423. Fixes rust-lang#116631.

This is my first contribution to this repo so please let me know if I'm supposed to change something :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet