-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
rustdoc: Stop unconditionally evaluating the initializer of associated consts #151232
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
rust-bors
merged 1 commit into
rust-lang:main
from
fmease:rustdoc-dont-eval-assoc-consts
Jan 17, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| <section id="associatedconstant.X" class="associatedconstant"><a class="src rightside" href="../src/foo/anchors.rs.html#42">Source</a><h4 class="code-header">pub const <a href="#associatedconstant.X" class="constant">X</a>: <a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a> = 0i32</h4></section> | ||
| <section id="associatedconstant.X" class="associatedconstant"><a class="src rightside" href="../src/foo/anchors.rs.html#42">Source</a><h4 class="code-header">pub const <a href="#associatedconstant.X" class="constant">X</a>: <a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a> = 0</h4></section> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
tests/rustdoc-html/constant/assoc-const-has-projection-ty.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Ensure that we properly print the value `1` as `1` in the initializer of associated constants | ||
| // that have user type "projection". | ||
| // | ||
| // We once used to evaluate the initializer in rustdoc and use rustc's MIR pretty-printer to | ||
| // render the resulting MIR const value. This pretty printer matches on the type to interpret | ||
| // the data and falls back to a cryptic `"{transmute(0x$data): $ty}"` for types it can't handle. | ||
| // Crucially, when constructing the MIR const we passed the unnormalized type of the initializer, | ||
| // i.e., the projection `<Struct as Trait>::Ty` instead of the normalized `u32` which the | ||
| // pretty printer obviously can't handle. | ||
| // | ||
| // Now we no longer evaluate it and use a custom printer for the const expr. | ||
| // | ||
| // issue: <https://github.com/rust-lang/rust/issues/150312> | ||
|
|
||
| #![crate_name = "it"] | ||
|
|
||
| pub trait Trait { | ||
| type Ty; | ||
|
|
||
| const CT: Self::Ty; | ||
| } | ||
|
|
||
| pub struct Struct; | ||
|
|
||
| impl Trait for Struct { | ||
| type Ty = u32; | ||
|
|
||
| //@ has it/struct.Struct.html | ||
| //@ has - '//*[@id="associatedconstant.CT"]' 'const CT: Self::Ty = 1' | ||
| const CT: Self::Ty = 1; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Ensure that we don't unconditionally evaluate the initializer of associated constants. | ||
| // | ||
| // We once used to evaluate them so we could display more kinds of expressions | ||
| // (like `1 + 1` as `2`) given the fact that we generally only want to render | ||
| // literals (otherwise we would risk dumping extremely large exprs or leaking | ||
| // private struct fields). | ||
| // | ||
| // However, that deviated from rustc's behavior, made rustdoc accept less code | ||
| // and was understandably surprising to users. So let's not. | ||
| // | ||
| // In the future we *might* provide users a mechanism to control this behavior. | ||
| // E.g., via a new `#[doc(...)]` attribute. | ||
| // | ||
| // See also: | ||
| // issue: <https://github.com/rust-lang/rust/issues/131625> | ||
| // issue: <https://github.com/rust-lang/rust/issues/149635> | ||
|
|
||
| //@ check-pass | ||
|
|
||
| pub struct Type; | ||
|
|
||
| impl Type { | ||
| pub const K0: () = panic!(); | ||
| pub const K1: std::convert::Infallible = loop {}; | ||
| } | ||
|
|
||
| pub trait Trait { | ||
| const K2: i32 = panic!(); | ||
| } | ||
|
|
||
| impl Trait for Type { | ||
| const K2: i32 = loop {}; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.