Skip to content

ICE: added error handle for values greater than 9999 in #140700

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 1 commit into
base: master
Choose a base branch
from

Conversation

Kivooeo
Copy link
Contributor

@Kivooeo Kivooeo commented May 6, 2025

removed panic in case where we do --explain > 9999 and added check for it

now error looks like this instead of ICE

$ rustc.exe --explain E10000
error: E10000 is not a valid error code

fixes #140647
r? @fmease

@rustbot
Copy link
Collaborator

rustbot commented May 6, 2025

fmease is on vacation.

Please choose another assignee.

@rustbot
Copy link
Collaborator

rustbot commented May 6, 2025

r? @davidtwco

rustbot has assigned @davidtwco.
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 May 6, 2025
@rust-log-analyzer

This comment has been minimized.

@fmease fmease assigned fmease and unassigned davidtwco May 6, 2025
@fmease fmease 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 May 6, 2025
@jieyouxu
Copy link
Member

jieyouxu commented May 6, 2025

I feel like this is very arbitrary. The 9999 limit is just an internal implementation detail (an internal "this is obviously wrong" check and would be bumped if we ever get to that many error codes) and shouldn't be exposed to the user. I would've just expected

$ rustc --explain E12345
error: E12345 is not a valid error code

to behave the same as

$ rustc --explain E4444
error: E4444 is not a valid error code

and

$ rustc --explain E12345678901234567890 # exceeds u32::MAX
error: E12345678901234567890 is not a valid error code

to gracefully fail with the same error message.

@Kivooeo
Copy link
Contributor Author

Kivooeo commented May 6, 2025

@jieyouxu any actual idea why tidy start fails here?

tidy check
tidy error: Error code `E9999` is used in the compiler but not defined and documented in `compiler/rustc_error_codes/src/lib.rs`.

like... should I just add E9999 there..?
and about your suggestion, you asking to merge both errors into one error_code is not a valid error code right?

@rustbot
Copy link
Collaborator

rustbot commented May 6, 2025

Some changes occurred in diagnostic error codes

cc @GuillaumeGomez

@Kivooeo
Copy link
Contributor Author

Kivooeo commented May 6, 2025

oh no, looks like adding E9999 was a miskate here
but it actually makes tidy pass, so idk, correct me here pls

Copy link
Member

@fmease fmease left a comment

Choose a reason for hiding this comment

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

any actual idea why tidy start fails here?

I'm pretty sure it's simply due to tidy (src/tools/tidy/src/error_codes.rs:361) grepping through ~all Rust compiler source files looking for \bE\d{4}\b and finding the E9999 in your previous error message that you've since removed.

@@ -547,6 +547,7 @@ E0801: 0801,
E0802: 0802,
E0803: 0803,
E0804: 0804,
E9999: 9999,
Copy link
Member

Choose a reason for hiding this comment

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

No, this shouldn't be necessary now that you've removed the specialized error message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah i removed this custom message error for anything greater than 9999 and removed E9999 from error_codes, ran tidy locally and tidy said it's fine, let's see

@rust-log-analyzer

This comment has been minimized.

@@ -7,7 +7,6 @@
use std::fmt;

rustc_index::newtype_index! {
#[max = 9999] // Because all error codes have four digits.
Copy link
Member

@fmease fmease May 6, 2025

Choose a reason for hiding this comment

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

While we obviously shouldn't crash when the user enters error codes greater than E9999, the original purpose of this internal limit is to prevent rustc devs from introducing five-digit error codes without changing all other relevant parts of the compiler, docs and tools (like tidy) to now pad all four-digit codes with an additional 0 to become five-digit codes, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i was wondering how can i fix this to make not panicking here, and without changing NewType entirely, so this is only solution that worked fine that i came up with

Copy link
Contributor

Choose a reason for hiding this comment

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

Running the ICE with debuginfo gives this line:

&& let Ok(description) = registry.try_find_description(ErrCode::from_u32(code))

You probably want to manually add an impl TryFrom<u32> for ErrCode.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

doesnt it will still panic inside NewType because of max limitations?

Copy link
Contributor Author

@Kivooeo Kivooeo May 6, 2025

Choose a reason for hiding this comment

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

i believe that the panic occurs here

assert!(value <= (#max as usize));

because ICE was adressing to this line

rustc_index::newtype_index! {

thread 'main' panicked at /rustc/d6a325d93a84077580c677f89affbe45682606c3\compiler\rustc_errors\src\codes.rs:9:1:
assertion failed: value <= 9999

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so, as far as i concerned, we cannot really prevent this panic when having #[max = 9999] and pass something bigger than it, we should not let this error code (that is bigger than max value) get to this assertations because it'll panic is such cases

but i may missing something for sure, if so please let me know we'll brainstorm it together

Copy link
Contributor

@ShE3py ShE3py May 6, 2025

Choose a reason for hiding this comment

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

The idea is to return Err(_)/None, i.e. not constructing the newtype if code > 9999 (= not calling from_u32, so the assert! is not reached).

Alternativement, adding impl Borrow<u32> for ErrCode so this fn can directly being called with an u32 (try_find_description<C>(&self, code: C) where ErrCode: Borrow<C>);

/// Returns `InvalidErrorCode` if the code requested does not exist in the
/// registry.
pub fn try_find_description(&self, code: ErrCode) -> Result<&'static str, InvalidErrorCode> {
self.long_descriptions.get(&code).copied().ok_or(InvalidErrorCode)
}

Alternatively, just adding && code <= ErrCode::MAX_AS_U32 before this:

&& let Ok(description) = registry.try_find_description(ErrCode::from_u32(code))

Copy link
Contributor Author

@Kivooeo Kivooeo May 6, 2025

Choose a reason for hiding this comment

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

yep, currently playing around this ideas, this would take some time because of recompilling, thanks for ideas

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ive tried this thing

  pub fn try_find_description(&self, code: ErrCode) -> Result<&'static str, InvalidErrorCode> {
      if code > ErrCode::from_u32(9999) {
          return Err(InvalidErrorCode);
      }
      self.long_descriptions.get(&code).copied().ok_or(InvalidErrorCode)
  }

and its not worked for me
but this one && code <= ErrCode::MAX_AS_U32 runs like perfect, so we can stick with it

@ShE3py
Copy link
Contributor

ShE3py commented May 6, 2025

You may want to add a regression test based on tests/ui/explain.rs

@Kivooeo
Copy link
Contributor Author

Kivooeo commented May 6, 2025

@ShE3py oh, yes i want to add tests for it, didnt knew that there is some for --explain, i should create it separate tests or can do it in one file?

yeah, i guess i cant do this in one file
error: Option 'explain' given more than once

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
Development

Successfully merging this pull request may close these issues.

rustc explain panics with error code longer than 4 digits
7 participants