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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. 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
4 participants