-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[rustdoc] Don't try to print the value of a non-primitive const #150629
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
base: main
Are you sure you want to change the base?
Conversation
|
|
|
@GuillaumeGomez are you free to take a look, maybe? |
|
NB: We might want to cease calling More concretely, for provided assoc consts we should just print |
|
Would you be interested in spinning up such an alternative PR? |
Yes! Not sure when I'll get to it, but I'll be happy to take a look. Thanks! |
| @@ -0,0 +1,18 @@ | |||
| #![crate_name = "foo"] | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also please add a comment explaining what this is testing (we should maybe enforce that in all tests).
|
Fix looks good to me but my knowledge in this part of the code is quite limited so letting @fmease handle it. ;) |
Hey @fmease, I'm messing around with this, and want to make sure you've taken the consequences into account: There are probably more examples even in the STD docs, but this is one that I found quite quickly: https://doc.rust-lang.org/stable/std/primitive.f32.html#associatedconstant.INFINITY |
|
Thanks for reaching out! Yes, indeed, those are the consequences unfortunately. However note that prior to PR #95316 (2022) we didn't show anything; now we would at least still show simple literals. If you think about it, starting to eagerly evaluate associated constants (even in generic contexts) was technically speaking a correctness regression. It's reasonable that users expect assoc consts to be evaluated post-mono & only when referenced just like during normal compilation. Currently we're rejecting valid patterns (like setting an assoc const to I knew about this issue for quite a while but I never got around to fixing it the way I'm proposing now because I've always hoped that we'd introduce new "control" |
b127b9f to
d03698f
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
@fmease took initiative and decided to try and also print marking as draft since it's not ready for proper review, but I would like to see if you like the direction this is going, specifically - if you could review the changes to the tests to see that this is what you meant. |
|
BTW, |
This comment has been minimized.
This comment has been minimized.
This comment was marked as resolved.
This comment was marked as resolved.
1 similar comment
This comment was marked as resolved.
This comment was marked as resolved.
bfe62e7 to
d6018cf
Compare
| let tcx = cx.tcx(); | ||
| render_attributes_in_code(w, it, "", cx)?; | ||
|
|
||
| let val = fmt::from_fn(|f| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that we could continue using c.value() for free const items that don't have non-lifetime (generic) parameters since rustc evaluates them already anyway. 🤷 As I've said in a bunch of comments, we definitely want to stop evaluating associated const items by default.
However, if you consider it not worth the complexity we can leave like this for the time being.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, sorry about that, managed to get a bit lost on the way :)
Let me try to implement it and we'll see just how complex it ends up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addendum
I've now actually reviewed the code & have realized that you've modified item_constant which is called for free const items only (for assoc consts, it's fn assoc_const in render/mod.rs).
I actually want to take a step back. Your PR was originally only wanted to fix #150312 (ValTree's dbg repr leaking through) but I steered you towards also addressing the "diverging assoc consts" issues while you're at it. As a result, it feels a bit tangled & confusing to me. I'm sorry about that.
I'll spin up a separate PR that exclusively fixes the new regression and doesn't try to restrict the kinds of const exprs since the latter doesn't have any time pressure.
Feel free to revert back to the initial version which I can then review w/o having the "divering assoc consts" issues in mind.
| matches!( | ||
| tcx.hir_node(hir_id), | ||
| Node::Expr(Expr { | ||
| kind: Lit(_) | Path(_) | Unary(UnOp::Neg, Expr { kind: Lit(_) | Path(_), .. }), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Path is hardly a primitive. We might want to rename this function to is_simple_expr.
| ) -> Option<String> { | ||
| tcx.const_eval_poly(def_id).ok().and_then(|val| { | ||
| let ty = tcx.type_of(def_id).instantiate_identity(); | ||
| if !ty.is_primitive() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't include all types that have literals, meaning it's stricter than is_literal_expr leading us to display fewer kinds of literals if extern compared to local.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having the filter after const_eval_poly means we're still evaluating assoc (and free) const items that were cross-crate inlined meaning it doesn't address the recent regression & its older "duplicates".
| let tcx = cx.tcx(); | ||
| render_attributes_in_code(w, it, "", cx)?; | ||
|
|
||
| let val = fmt::from_fn(|f| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addendum
I've now actually reviewed the code & have realized that you've modified item_constant which is called for free const items only (for assoc consts, it's fn assoc_const in render/mod.rs).
I actually want to take a step back. Your PR was originally only wanted to fix #150312 (ValTree's dbg repr leaking through) but I steered you towards also addressing the "diverging assoc consts" issues while you're at it. As a result, it feels a bit tangled & confusing to me. I'm sorry about that.
I'll spin up a separate PR that exclusively fixes the new regression and doesn't try to restrict the kinds of const exprs since the latter doesn't have any time pressure.
Feel free to revert back to the initial version which I can then review w/o having the "divering assoc consts" issues in mind.
|
Let's wait for my "diverging assoc consts" PR, so we can see what's truly needed afterwards. Thanks for your understanding :) @rustbot blocked |
…mofek rustdoc: Stop unconditionally evaluating the initializer of associated consts See the descriptions of the added tests for details. Fixes #131625. Fixes [after beta-1.93 backport] #149635. Fixes #150312. Supersedes #150629 IINM. CC @cuviper (#149635 (comment)) r? @GuillaumeGomez or @yotamofek (#150629)
r? @fmease maybe? (feel free to re-assign, ofc 😁)
Pretty new to these parts of the code, so I have no idea if this is an acceptable fix.
Fixes #150312