Skip to content

Make Error::type_id() an inherent method on dyn Error#156252

Open
Jules-Bertholet wants to merge 1 commit into
rust-lang:mainfrom
Jules-Bertholet:error-typeid-inherent
Open

Make Error::type_id() an inherent method on dyn Error#156252
Jules-Bertholet wants to merge 1 commit into
rust-lang:mainfrom
Jules-Bertholet:error-typeid-inherent

Conversation

@Jules-Bertholet
Copy link
Copy Markdown
Contributor

@Jules-Bertholet Jules-Bertholet commented May 6, 2026

Resolves the soundness issues with this method, and ensures we don't rely on #57893 weirdness in a user-visible way.

Previously suggested by @seanmonstar: #60784 (comment)

Tracking issue: #60784

@rustbot label T-libs-api A-error-handling PG-error-handling

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 6, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 6, 2026

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @scottmcm, libs
  • @scottmcm, libs expanded to 8 candidates
  • Random selection from Mark-Simulacrum, jhpratt, nia-e, scottmcm

@rustbot rustbot added A-error-handling Area: Error handling PG-error-handling Project group: Error handling (https://github.com/rust-lang/project-error-handling) T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels May 6, 2026
@rustbot

This comment has been minimized.

Resolves the soundness issues with this method,
and ensures we don't rely on trait object method
resolution weirdness in a user-visible way.
@Jules-Bertholet Jules-Bertholet force-pushed the error-typeid-inherent branch from c08d3cc to 3f6f5e0 Compare May 6, 2026 19:16
Comment thread library/core/src/error.rs
issue = "none"
)]
fn type_id(&self, _: private::Internal) -> TypeId
fn __type_id(&self) -> TypeId
Copy link
Copy Markdown
Member

@Mark-Simulacrum Mark-Simulacrum May 10, 2026

Choose a reason for hiding this comment

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

What am I missing for why this doesn't allow users to unsoundly override this, i.e., the point of having the private::Internal here previously?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's what the perma-unstable feature gate is for.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There was an argument against allowing it to be just unstable in the original PR, that people using nightly can still make unsound things.

Copy link
Copy Markdown
Contributor Author

@Jules-Bertholet Jules-Bertholet May 10, 2026

Choose a reason for hiding this comment

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

To make an unsound thing with this, you would have to not only use nightly, but also look through the source code, see the doc(hidden) item, and then choose to enable the feature gate despite the warning. If you are that determined to shoot yourself in the foot, Rust cannot stop you, and there are plenty of other feature gates you could use instead. For example, pin!() used to rely on a feature gate much like this (removed by #139114).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think I agree, but the PR description reads like this addresses that soundness problem - it's not obvious to me how this is an improvement over what's on main right now...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

After this PR, the fn type_id() method on dyn Error could be soundly stabilized, as it's impossible to override it. Only the __type_id() method on the trait itself needs to remain unstable, and that method is not particularly useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-error-handling Area: Error handling PG-error-handling Project group: Error handling (https://github.com/rust-lang/project-error-handling) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants