Skip to content

rustdoc: correct negative-to-implicit discriminant display #145216

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 2 commits into from
Aug 12, 2025

Conversation

eval-exec
Copy link
Contributor

@eval-exec eval-exec commented Aug 10, 2025

This PR want to fix #145125

In:

#[derive(Copy, Clone, Debug)]
pub struct Discr<'tcx> {
/// Bit representation of the discriminant (e.g., `-1i8` is `0xFF_u128`).
pub val: u128,
pub ty: Ty<'tcx>,
}

the Discr's val field is u128, so we can't use discr.val as i128 to represent Discr's signed value.

We should use Discr's Display trait to display signed value.

impl<'tcx> fmt::Display for Discr<'tcx> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self.ty.kind() {
ty::Int(ity) => {
let size = ty::tls::with(|tcx| Integer::from_int_ty(&tcx, ity).size());
let x = self.val;
// sign extend the raw representation to be an i128
let x = size.sign_extend(x) as i128;
write!(fmt, "{x}")
}
_ => write!(fmt, "{}", self.val),
}
}
}

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. labels Aug 10, 2025
@eval-exec eval-exec marked this pull request as ready for review August 10, 2025 15:35
@rustbot
Copy link
Collaborator

rustbot commented Aug 10, 2025

r? @notriddle

rustbot has assigned @notriddle.
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. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 10, 2025
@fmease fmease assigned fmease and unassigned notriddle Aug 10, 2025
@eval-exec eval-exec changed the title Fix Rustdoc integer overflow when calculating negative fieldless enum value rustdoc: correct negative-to-implicit discriminant display (-1 shown as u128 max Aug 10, 2025
@eval-exec eval-exec changed the title rustdoc: correct negative-to-implicit discriminant display (-1 shown as u128 max rustdoc: correct negative-to-implicit discriminant display Aug 10, 2025
Copy link
Contributor

@lolbinarycat lolbinarycat left a comment

Choose a reason for hiding this comment

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

LGTM, test passes in CI and fails when run against current master, code change seems quite simple.

@fmease
Copy link
Member

fmease commented Aug 11, 2025

Thanks! Yup, @bors r+ rollup

@bors
Copy link
Collaborator

bors commented Aug 11, 2025

📌 Commit 20a134f has been approved by fmease

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 Aug 11, 2025
fmease added a commit to fmease/rust that referenced this pull request Aug 11, 2025
… r=fmease

rustdoc: correct negative-to-implicit discriminant display

This PR want to fix rust-lang#145125

In:
https://github.com/rust-lang/rust/blob/7f7b8ef27d86c865a7ab20c7c42f50811c6a914d/compiler/rustc_middle/src/ty/util.rs#L33-L38

the `Discr`'s `val` field is `u128`, so we can't use `discr.val as i128` to represent `Discr`'s signed value.

We should use `Discr`'s `Display` trait to display signed value.

https://github.com/rust-lang/rust/blob/7f7b8ef27d86c865a7ab20c7c42f50811c6a914d/compiler/rustc_middle/src/ty/util.rs#L60-L73
fmease added a commit to fmease/rust that referenced this pull request Aug 12, 2025
… r=fmease

rustdoc: correct negative-to-implicit discriminant display

This PR want to fix rust-lang#145125

In:
https://github.com/rust-lang/rust/blob/7f7b8ef27d86c865a7ab20c7c42f50811c6a914d/compiler/rustc_middle/src/ty/util.rs#L33-L38

the `Discr`'s `val` field is `u128`, so we can't use `discr.val as i128` to represent `Discr`'s signed value.

We should use `Discr`'s `Display` trait to display signed value.

https://github.com/rust-lang/rust/blob/7f7b8ef27d86c865a7ab20c7c42f50811c6a914d/compiler/rustc_middle/src/ty/util.rs#L60-L73
fmease added a commit to fmease/rust that referenced this pull request Aug 12, 2025
… r=fmease

rustdoc: correct negative-to-implicit discriminant display

This PR want to fix rust-lang#145125

In:
https://github.com/rust-lang/rust/blob/7f7b8ef27d86c865a7ab20c7c42f50811c6a914d/compiler/rustc_middle/src/ty/util.rs#L33-L38

the `Discr`'s `val` field is `u128`, so we can't use `discr.val as i128` to represent `Discr`'s signed value.

We should use `Discr`'s `Display` trait to display signed value.

https://github.com/rust-lang/rust/blob/7f7b8ef27d86c865a7ab20c7c42f50811c6a914d/compiler/rustc_middle/src/ty/util.rs#L60-L73
fmease added a commit to fmease/rust that referenced this pull request Aug 12, 2025
… r=fmease

rustdoc: correct negative-to-implicit discriminant display

This PR want to fix rust-lang#145125

In:
https://github.com/rust-lang/rust/blob/7f7b8ef27d86c865a7ab20c7c42f50811c6a914d/compiler/rustc_middle/src/ty/util.rs#L33-L38

the `Discr`'s `val` field is `u128`, so we can't use `discr.val as i128` to represent `Discr`'s signed value.

We should use `Discr`'s `Display` trait to display signed value.

https://github.com/rust-lang/rust/blob/7f7b8ef27d86c865a7ab20c7c42f50811c6a914d/compiler/rustc_middle/src/ty/util.rs#L60-L73
bors added a commit that referenced this pull request Aug 12, 2025
Rollup of 15 pull requests

Successful merges:

 - #131477 (Apple: Always pass SDK root when linking with `cc`, and pass it via `SDKROOT` env var)
 - #139806 (std: sys: pal: uefi: Overhaul Time)
 - #144386 (Extract TraitImplHeader in AST/HIR)
 - #144542 (Stabilize `sse4a` and `tbm` target features)
 - #144921 (Don't emit `rustdoc::broken_intra_doc_links` for GitHub-flavored Markdown admonitions like `[!NOTE]`)
 - #145155 (Port `#[allow_internal_unsafe]` to the new attribute system (attempt 2))
 - #145214 (fix: re-enable self-assignment)
 - #145216 (rustdoc: correct negative-to-implicit discriminant display)
 - #145238 (Tweak invalid builtin attribute output)
 - #145249 (Rename entered trace span variables from `_span` to  `_trace`)
 - #145251 (Support using #[unstable_feature_bound] on trait)
 - #145253 (Document compiler and stdlib in stage1 in `pr-check-2` CI job)
 - #145260 (Make explicit guarantees about `Vec`’s allocator)
 - #145263 (Update books)
 - #145273 (Account for new `assert!` desugaring in `!condition` suggestion)

r? `@ghost`
`@rustbot` modify labels: rollup
fmease added a commit to fmease/rust that referenced this pull request Aug 12, 2025
… r=fmease

rustdoc: correct negative-to-implicit discriminant display

This PR want to fix rust-lang#145125

In:
https://github.com/rust-lang/rust/blob/7f7b8ef27d86c865a7ab20c7c42f50811c6a914d/compiler/rustc_middle/src/ty/util.rs#L33-L38

the `Discr`'s `val` field is `u128`, so we can't use `discr.val as i128` to represent `Discr`'s signed value.

We should use `Discr`'s `Display` trait to display signed value.

https://github.com/rust-lang/rust/blob/7f7b8ef27d86c865a7ab20c7c42f50811c6a914d/compiler/rustc_middle/src/ty/util.rs#L60-L73
bors added a commit that referenced this pull request Aug 12, 2025
Rollup of 14 pull requests

Successful merges:

 - #131477 (Apple: Always pass SDK root when linking with `cc`, and pass it via `SDKROOT` env var)
 - #139806 (std: sys: pal: uefi: Overhaul Time)
 - #144210 (std: thread: Return error if setting thread stack size fails)
 - #144386 (Extract TraitImplHeader in AST/HIR)
 - #144921 (Don't emit `rustdoc::broken_intra_doc_links` for GitHub-flavored Markdown admonitions like `[!NOTE]`)
 - #145155 (Port `#[allow_internal_unsafe]` to the new attribute system (attempt 2))
 - #145214 (fix: re-enable self-assignment)
 - #145216 (rustdoc: correct negative-to-implicit discriminant display)
 - #145238 (Tweak invalid builtin attribute output)
 - #145249 (Rename entered trace span variables from `_span` to  `_trace`)
 - #145251 (Support using #[unstable_feature_bound] on trait)
 - #145253 (Document compiler and stdlib in stage1 in `pr-check-2` CI job)
 - #145263 (Update books)
 - #145273 (Account for new `assert!` desugaring in `!condition` suggestion)

r? `@ghost`
`@rustbot` modify labels: rollup
Zalathar added a commit to Zalathar/rust that referenced this pull request Aug 12, 2025
… r=fmease

rustdoc: correct negative-to-implicit discriminant display

This PR want to fix rust-lang#145125

In:
https://github.com/rust-lang/rust/blob/7f7b8ef27d86c865a7ab20c7c42f50811c6a914d/compiler/rustc_middle/src/ty/util.rs#L33-L38

the `Discr`'s `val` field is `u128`, so we can't use `discr.val as i128` to represent `Discr`'s signed value.

We should use `Discr`'s `Display` trait to display signed value.

https://github.com/rust-lang/rust/blob/7f7b8ef27d86c865a7ab20c7c42f50811c6a914d/compiler/rustc_middle/src/ty/util.rs#L60-L73
bors added a commit that referenced this pull request Aug 12, 2025
Rollup of 17 pull requests

Successful merges:

 - #131477 (Apple: Always pass SDK root when linking with `cc`, and pass it via `SDKROOT` env var)
 - #139806 (std: sys: pal: uefi: Overhaul Time)
 - #144210 (std: thread: Return error if setting thread stack size fails)
 - #144386 (Extract TraitImplHeader in AST/HIR)
 - #144921 (Don't emit `rustdoc::broken_intra_doc_links` for GitHub-flavored Markdown admonitions like `[!NOTE]`)
 - #145155 (Port `#[allow_internal_unsafe]` to the new attribute system (attempt 2))
 - #145214 (fix: re-enable self-assignment)
 - #145216 (rustdoc: correct negative-to-implicit discriminant display)
 - #145238 (Tweak invalid builtin attribute output)
 - #145249 (Rename entered trace span variables from `_span` to  `_trace`)
 - #145251 (Support using #[unstable_feature_bound] on trait)
 - #145253 (Document compiler and stdlib in stage1 in `pr-check-2` CI job)
 - #145260 (Make explicit guarantees about `Vec`’s allocator)
 - #145263 (Update books)
 - #145273 (Account for new `assert!` desugaring in `!condition` suggestion)
 - #145283 (Make I-miscompile imply I-prioritize)
 - #145291 (bootstrap: Only warn about `rust.debug-assertions` if downloading rustc)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit that referenced this pull request Aug 12, 2025
Rollup of 17 pull requests

Successful merges:

 - #131477 (Apple: Always pass SDK root when linking with `cc`, and pass it via `SDKROOT` env var)
 - #139806 (std: sys: pal: uefi: Overhaul Time)
 - #144386 (Extract TraitImplHeader in AST/HIR)
 - #144921 (Don't emit `rustdoc::broken_intra_doc_links` for GitHub-flavored Markdown admonitions like `[!NOTE]`)
 - #145155 (Port `#[allow_internal_unsafe]` to the new attribute system (attempt 2))
 - #145214 (fix: re-enable self-assignment)
 - #145216 (rustdoc: correct negative-to-implicit discriminant display)
 - #145238 (Tweak invalid builtin attribute output)
 - #145249 (Rename entered trace span variables from `_span` to  `_trace`)
 - #145251 (Support using #[unstable_feature_bound] on trait)
 - #145253 (Document compiler and stdlib in stage1 in `pr-check-2` CI job)
 - #145260 (Make explicit guarantees about `Vec`’s allocator)
 - #145263 (Update books)
 - #145273 (Account for new `assert!` desugaring in `!condition` suggestion)
 - #145283 (Make I-miscompile imply I-prioritize)
 - #145291 (bootstrap: Only warn about `rust.debug-assertions` if downloading rustc)
 - #145292 (Fix a typo in range docs)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 38483d8 into rust-lang:master Aug 12, 2025
10 checks passed
@rustbot rustbot added this to the 1.91.0 milestone Aug 12, 2025
rust-timer added a commit that referenced this pull request Aug 12, 2025
Rollup merge of #145216 - eval-exec:fix-145125-enum-rustdoc, r=fmease

rustdoc: correct negative-to-implicit discriminant display

This PR want to fix #145125

In:
https://github.com/rust-lang/rust/blob/7f7b8ef27d86c865a7ab20c7c42f50811c6a914d/compiler/rustc_middle/src/ty/util.rs#L33-L38

the `Discr`'s `val` field is `u128`, so we can't use `discr.val as i128` to represent `Discr`'s signed value.

We should use `Discr`'s `Display` trait to display signed value.

https://github.com/rust-lang/rust/blob/7f7b8ef27d86c865a7ab20c7c42f50811c6a914d/compiler/rustc_middle/src/ty/util.rs#L60-L73
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Aug 13, 2025
Rollup of 17 pull requests

Successful merges:

 - rust-lang/rust#131477 (Apple: Always pass SDK root when linking with `cc`, and pass it via `SDKROOT` env var)
 - rust-lang/rust#139806 (std: sys: pal: uefi: Overhaul Time)
 - rust-lang/rust#144386 (Extract TraitImplHeader in AST/HIR)
 - rust-lang/rust#144921 (Don't emit `rustdoc::broken_intra_doc_links` for GitHub-flavored Markdown admonitions like `[!NOTE]`)
 - rust-lang/rust#145155 (Port `#[allow_internal_unsafe]` to the new attribute system (attempt 2))
 - rust-lang/rust#145214 (fix: re-enable self-assignment)
 - rust-lang/rust#145216 (rustdoc: correct negative-to-implicit discriminant display)
 - rust-lang/rust#145238 (Tweak invalid builtin attribute output)
 - rust-lang/rust#145249 (Rename entered trace span variables from `_span` to  `_trace`)
 - rust-lang/rust#145251 (Support using #[unstable_feature_bound] on trait)
 - rust-lang/rust#145253 (Document compiler and stdlib in stage1 in `pr-check-2` CI job)
 - rust-lang/rust#145260 (Make explicit guarantees about `Vec`’s allocator)
 - rust-lang/rust#145263 (Update books)
 - rust-lang/rust#145273 (Account for new `assert!` desugaring in `!condition` suggestion)
 - rust-lang/rust#145283 (Make I-miscompile imply I-prioritize)
 - rust-lang/rust#145291 (bootstrap: Only warn about `rust.debug-assertions` if downloading rustc)
 - rust-lang/rust#145292 (Fix a typo in range docs)

r? `@ghost`
`@rustbot` modify labels: rollup
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-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rustdoc integer overflow when calculating negative fieldless enum value
6 participants