-
-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -371,6 +371,9 @@ pub(crate) fn print_evaluated_const( | |
| ) -> 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() { | ||
| return None; | ||
| } | ||
| match (val, ty.kind()) { | ||
| (_, &ty::Ref(..)) => None, | ||
| (mir::ConstValue::Scalar(_), &ty::Adt(_, _)) => None, | ||
|
|
@@ -439,19 +442,16 @@ fn print_const_with_custom_print_scalar<'tcx>( | |
| } | ||
|
|
||
| pub(crate) fn is_literal_expr(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool { | ||
| if let hir::Node::Expr(expr) = tcx.hir_node(hir_id) { | ||
| if let hir::ExprKind::Lit(_) = &expr.kind { | ||
| return true; | ||
| } | ||
|
|
||
| if let hir::ExprKind::Unary(hir::UnOp::Neg, expr) = &expr.kind | ||
| && let hir::ExprKind::Lit(_) = &expr.kind | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| false | ||
| use ExprKind::{Lit, Path, Unary}; | ||
| use hir::{Expr, ExprKind, Node, UnOp}; | ||
|
|
||
| matches!( | ||
| tcx.hir_node(hir_id), | ||
| Node::Expr(Expr { | ||
| kind: Lit(_) | Path(_) | Unary(UnOp::Neg, Expr { kind: Lit(_) | Path(_), .. }), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| .. | ||
| }) | ||
| ) | ||
| } | ||
|
|
||
| /// Given a type Path, resolve it to a Type using the TyCtxt | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1920,9 +1920,19 @@ fn item_constant( | |
| let tcx = cx.tcx(); | ||
| render_attributes_in_code(w, it, "", cx)?; | ||
|
|
||
| let val = fmt::from_fn(|f| { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that we could continue using However, if you consider it not worth the complexity we can leave like this for the time being.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 :)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AddendumI've now actually reviewed the code & have realized that you've modified 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 is_literal = c.is_literal(tcx); | ||
| let expr = c.expr(tcx); | ||
| if is_literal { | ||
| write!(f, " = {expr}", expr = Escape(&expr)) | ||
| } else { | ||
| f.write_str(" = _") | ||
| } | ||
| }); | ||
|
|
||
| write!( | ||
| w, | ||
| "{vis}const {name}{generics}: {typ}{where_clause}", | ||
| "{vis}const {name}{generics}: {typ}{val}{where_clause};", | ||
| vis = visibility_print_with_space(it, cx), | ||
| name = it.name.unwrap(), | ||
| generics = print_generics(generics, cx), | ||
|
|
@@ -1931,34 +1941,6 @@ fn item_constant( | |
| print_where_clause(generics, cx, 0, Ending::NoNewline).maybe_display(), | ||
| )?; | ||
|
|
||
| // FIXME: The code below now prints | ||
| // ` = _; // 100i32` | ||
| // if the expression is | ||
| // `50 + 50` | ||
| // which looks just wrong. | ||
| // Should we print | ||
| // ` = 100i32;` | ||
| // instead? | ||
|
|
||
| let value = c.value(tcx); | ||
| let is_literal = c.is_literal(tcx); | ||
| let expr = c.expr(tcx); | ||
| if value.is_some() || is_literal { | ||
| write!(w, " = {expr};", expr = Escape(&expr))?; | ||
| } else { | ||
| w.write_str(";")?; | ||
| } | ||
|
|
||
| if !is_literal && let Some(value) = &value { | ||
| let value_lowercase = value.to_lowercase(); | ||
| let expr_lowercase = expr.to_lowercase(); | ||
|
|
||
| if value_lowercase != expr_lowercase | ||
| && value_lowercase.trim_end_matches("i32") != expr_lowercase | ||
| { | ||
| write!(w, " // {value}", value = Escape(value))?; | ||
| } | ||
| } | ||
| Ok::<(), fmt::Error>(()) | ||
| })?; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| #![crate_name = "foo"] | ||
|
|
||
| //@ has 'foo/constant.HOUR_IN_SECONDS.html' | ||
| //@ has - '//*[@class="rust item-decl"]//code' 'pub const HOUR_IN_SECONDS: u64 = _; // 3_600u64' | ||
| //@ has - '//*[@class="rust item-decl"]//code' 'pub const HOUR_IN_SECONDS: u64 = _;' | ||
| pub const HOUR_IN_SECONDS: u64 = 60 * 60; | ||
|
|
||
| //@ has 'foo/constant.NEGATIVE.html' | ||
| //@ has - '//*[@class="rust item-decl"]//code' 'pub const NEGATIVE: i64 = _; // -3_600i64' | ||
| //@ has - '//*[@class="rust item-decl"]//code' 'pub const NEGATIVE: i64 = _;' | ||
| pub const NEGATIVE: i64 = -60 * 60; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #![crate_name = "foo"] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). |
||
|
|
||
| pub trait Foo { | ||
| type AssocType; | ||
|
|
||
| const THE_CONST: Self::AssocType; | ||
| } | ||
|
|
||
| pub struct Bar; | ||
|
|
||
| impl Foo for Bar { | ||
| type AssocType = (u32,); | ||
|
|
||
| //@ has foo/struct.Bar.html | ||
| //@ matches - '//section[@id="associatedconstant.THE_CONST"]/h4[@class="code-header"]' '^const THE_CONST: Self::AssocType' | ||
| //@ !matches - '//section[@id="associatedconstant.THE_CONST"]//*[last()]' '{transmute' | ||
| const THE_CONST: Self::AssocType = (1u32,); | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| //@ check-pass | ||
| //@ only-64bit | ||
|
|
||
| #![feature(const_transmute)] | ||
|
|
||
| pub const ZST: &[u8] = unsafe { std::mem::transmute(1usize) }; | ||
| //~^ ERROR transmuting from 8-byte type to 16-byte type |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,7 @@ | ||
| //@ check-pass | ||
| //@ compile-flags: -Z track-diagnostics | ||
|
|
||
| // Normalize the emitted location so this doesn't need | ||
| // updating everytime someone adds or removes a line. | ||
| //@ normalize-stderr: ".rs:\d+:\d+" -> ".rs:LL:CC" | ||
|
|
||
| struct A; | ||
| struct B; | ||
|
|
||
| pub const S: A = B; | ||
| //~^ ERROR mismatched types | ||
| //~| NOTE created at | ||
| //~| NOTE expected `A`, found `B` |
This file was deleted.
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_exprleading 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_polymeans 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".