Skip to content

Modifies the rendering of constant values in rustdoc. #112167

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

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1491,22 +1491,23 @@ fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &cle
let value = c.value(tcx);
let is_literal = c.is_literal(tcx);
let expr = c.expr(tcx);
if value.is_some() || is_literal {
if is_literal {
write!(w, " = {expr};", expr = Escape(&expr));
} else if let Some(ref value) = value {
write!(w, " = {value};", value = Escape(value));
} else {
w.write_str(";");
}

if !is_literal {
if let Some(value) = &value {
let value_lowercase = value.to_lowercase();
let expr_lowercase = expr.to_lowercase();
let value_lowercase = value.as_ref().map(|s| s.to_lowercase());
Copy link
Member

Choose a reason for hiding this comment

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

In your earlier PR I meant to suggest removing the entire branch including the body (the // business) not just the if-condition, sorry if that wasn't clear. This here still prints 3i32 // 3i32 for 1 + 2 if I'm not mistaken.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct that makes sense, sorry if I might not have understood it earlier.

13: @!hasraw check failed
	`PATTERN` did not match
	// @!hasraw show_const_contents/constant.CONST_I32_HEX.html ';'
50: @hasraw check failed
	`PATTERN` did not match
	// @hasraw show_const_contents/constant.PI.html '= 3.14159265358979323846264338327950288f32;'
51: @hasraw check failed
	`PATTERN` did not match
	// @hasraw show_const_contents/constant.PI.html '; // 3.14159274f32'

These tests still seem to be failing, should the code be suggesting something else, I am a bit confused on what the correct comment should be here

let expr_lowercase = Some(expr.to_lowercase());

if value_lowercase != expr_lowercase
&& value_lowercase.trim_end_matches("i32") != expr_lowercase
{
write!(w, " // {value}", value = Escape(value));
}
if value_lowercase != expr_lowercase
&& value_lowercase.as_ref().map(|s| s.trim_end_matches("i32"))
!= expr_lowercase.as_deref()
{
if let Some(ref value) = value {
write!(w, " // {value}", value = Escape(value.as_str()));
}
}
});
Expand Down