Skip to content
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

rustdoc: Cleanup clean part 2 #88895

Merged
merged 3 commits into from
Sep 26, 2021
Merged

rustdoc: Cleanup clean part 2 #88895

merged 3 commits into from
Sep 26, 2021

Conversation

camelid
Copy link
Member

@camelid camelid commented Sep 12, 2021

Split out from #88379. This contains the following commits from that PR:

  • Remove Type::ResolvedPath.is_generic
  • Rename is_generic() to is_assoc_ty()

r? @jyn514

It can be computed on-demand.
The new name is more accurate than the previous one.
@camelid camelid added C-cleanup Category: PRs that clean code up or issues documenting cleanup. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Sep 12, 2021
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 12, 2021
Copy link
Member Author

@camelid camelid left a comment

Choose a reason for hiding this comment

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

I left explanatory comments on each nontrivial part of the diff to hopefully make this change easier to review :)

Comment on lines +405 to 414
match path.res {
Res::PrimTy(p) => Primitive(PrimitiveType::from(p)),
Res::SelfTy(..) if path.segments.len() == 1 => Generic(kw::SelfUpper),
Res::Def(DefKind::TyParam, _) if path.segments.len() == 1 => Generic(path.segments[0].name),
_ => {
let did = register_res(cx, path.res);
ResolvedPath { path, did }
}
Res::SelfTy(..) | Res::Def(DefKind::TyParam | DefKind::AssocTy, _) => true,
_ => false,
};
let did = register_res(cx, path.res);
ResolvedPath { path, did, is_generic }
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

The part of this code that computed is_generic is now covered by Path::is_assoc_ty(), so I removed it.

Comment on lines +1993 to +2000
crate fn is_assoc_ty(&self) -> bool {
match self.res {
Res::SelfTy(..) if self.segments.len() != 1 => true,
Res::Def(DefKind::TyParam, _) if self.segments.len() != 1 => true,
Res::Def(DefKind::AssocTy, _) => true,
_ => false,
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This code is adapted directly from resolve_type().

Comment on lines -1117 to -1120
PolyTrait {
trait_: ResolvedPath { path, did, is_generic: false },
generic_params: Vec::new(),
},
Copy link
Member Author

Choose a reason for hiding this comment

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

This path belongs to the Sized trait, so it's not an associated type, and Path::is_assoc_ty should preserve that because the Res should be "correct by construction".

Comment on lines -1493 to -1496
PolyTrait {
trait_: ResolvedPath { path, did, is_generic: false },
generic_params: Vec::new(),
},
Copy link
Member Author

Choose a reason for hiding this comment

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

This path is for the first trait bound in a dyn Trait1 + Trait2 + ... object, so its Res should be correct and preserve the is_generic: false behavior.

@@ -1473,7 +1473,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
let path = external_path(cx, did, false, vec![], empty);
inline::record_extern_fqn(cx, did, ItemType::Trait);
let bound = PolyTrait {
trait_: ResolvedPath { path, did, is_generic: false },
Copy link
Member Author

Choose a reason for hiding this comment

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

This path is for one of the trait bounds after the first (i.e., one of the auto trait bounds) in a dyn Trait1 + Trait2 + ... object, so its Res should be correct and preserve the is_generic: false behavior.

@@ -1442,12 +1442,12 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
};
inline::record_extern_fqn(cx, did, kind);
let path = external_path(cx, did, false, vec![], substs);
ResolvedPath { path, did, is_generic: false }
Copy link
Member Author

Choose a reason for hiding this comment

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

This path is for a struct/enum/union declaration, so its Res should be correct and preserve the is_generic: false behavior.

@@ -168,7 +168,7 @@ impl Clean<Type> for (ty::TraitRef<'_>, &[TypeBinding]) {

debug!("ty::TraitRef\n subst: {:?}\n", trait_ref.substs);

ResolvedPath { path, did: trait_ref.def_id, is_generic: false }
Copy link
Member Author

Choose a reason for hiding this comment

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

This path is for a trait in a ty::TraitRef (e.g., a where clause trait bound), so its Res should be correct and preserve the is_generic: false behavior.

Comment on lines +1412 to +1417
clean::ResolvedPath { ref path, .. }
| clean::BorrowedRef { type_: box clean::ResolvedPath { ref path, .. }, .. }
if !path.is_assoc_ty() =>
{
implementor_dups[&path.last()].1
}
Copy link
Member Author

Choose a reason for hiding this comment

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

is_assoc_ty() is the replacement for is_generic == true; this is the negation of that.

Comment on lines +715 to +718
clean::ResolvedPath { ref path, did, .. }
| clean::BorrowedRef {
type_: box clean::ResolvedPath { ref path, did, is_generic: false, .. },
..
} => {
type_: box clean::ResolvedPath { ref path, did, .. }, ..
} if !path.is_assoc_ty() => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Same as the above comment.

primitive_link(f, clean::PrimitiveType::RawPointer, &text, cx)
} else {
primitive_link(f, clean::PrimitiveType::RawPointer, &format!("*{} ", m), cx)?;
fmt::Display::fmt(&t.print(cx), f)
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I made a couple of cleanup changes here to reduce rightward drift and make the code easier to understand:

  • replaced match with if matches!
  • extracted a text local variable

The only other change is replacing the pattern clean::Generic(_) | clean::ResolvedPath { is_generic: true, .. } with the conditional if matches!(**t, clean::Generic(_)) || t.is_assoc_ty()), which is necessary now that the is_generic field has been removed in favor of using an is_assoc_ty() function.

Copy link
Member

@jyn514 jyn514 left a comment

Choose a reason for hiding this comment

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

This is awesome ❤️ I just have a couple nits.

src/librustdoc/clean/types.rs Show resolved Hide resolved
src/librustdoc/html/format.rs Outdated Show resolved Hide resolved
@jyn514 jyn514 added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 25, 2021
It's adapted from the old documentation for the `is_generic` field.
@camelid camelid added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 25, 2021
@camelid
Copy link
Member Author

camelid commented Sep 25, 2021

I re-added the documentation, but I did not remove the ** since it seems like it might require larger changes.

@jyn514
Copy link
Member

jyn514 commented Sep 25, 2021

@bors r+

Thanks!

@bors
Copy link
Contributor

bors commented Sep 25, 2021

📌 Commit dace2ee has been approved by jyn514

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 25, 2021
@bors
Copy link
Contributor

bors commented Sep 26, 2021

⌛ Testing commit dace2ee with merge b05ffbcff9af79f70d6f5889c7ec7a784ee934d0...

@jyn514
Copy link
Member

jyn514 commented Sep 26, 2021

@bors retry yield to #89262

bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 26, 2021
…arth

Rollup of 7 pull requests

Successful merges:

 - rust-lang#88895 (rustdoc: Cleanup `clean` part 2)
 - rust-lang#88973 (Expose the std_detect env_override feature)
 - rust-lang#89010 (Add some intra doc links)
 - rust-lang#89198 (rustdoc: Don't show hidden trait methods)
 - rust-lang#89216 (Consistent big O notation)
 - rust-lang#89224 (Change the order of imports suggestions)
 - rust-lang#89256 (Fix typo in release notes)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@rust-log-analyzer
Copy link
Collaborator

A job failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

@bors bors merged commit 7777f95 into rust-lang:master Sep 26, 2021
@rustbot rustbot added this to the 1.57.0 milestone Sep 26, 2021
@camelid camelid deleted the cleanup-pt2 branch September 26, 2021 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-cleanup Category: PRs that clean code up or issues documenting cleanup. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants