-
Notifications
You must be signed in to change notification settings - Fork 13.4k
small TypeVisitor
refactor
#79773
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
Merged
small TypeVisitor
refactor
#79773
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 |
---|---|---|
|
@@ -462,39 +462,25 @@ pub(super) fn check_opaque<'tcx>( | |
|
||
/// Checks that an opaque type does not use `Self` or `T::Foo` projections that would result | ||
/// in "inheriting lifetimes". | ||
#[instrument(skip(tcx, span))] | ||
pub(super) fn check_opaque_for_inheriting_lifetimes( | ||
tcx: TyCtxt<'tcx>, | ||
def_id: LocalDefId, | ||
span: Span, | ||
) { | ||
let item = tcx.hir().expect_item(tcx.hir().local_def_id_to_hir_id(def_id)); | ||
debug!( | ||
"check_opaque_for_inheriting_lifetimes: def_id={:?} span={:?} item={:?}", | ||
def_id, span, item | ||
); | ||
|
||
#[derive(Debug)] | ||
struct ProhibitOpaqueVisitor<'tcx> { | ||
opaque_identity_ty: Ty<'tcx>, | ||
generics: &'tcx ty::Generics, | ||
} | ||
debug!(?item, ?span); | ||
lcnr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
impl<'tcx> ty::fold::TypeVisitor<'tcx> for ProhibitOpaqueVisitor<'tcx> { | ||
type BreakTy = Option<Ty<'tcx>>; | ||
|
||
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> { | ||
debug!("check_opaque_for_inheriting_lifetimes: (visit_ty) t={:?}", t); | ||
if t != self.opaque_identity_ty && t.super_visit_with(self).is_break() { | ||
return ControlFlow::Break(Some(t)); | ||
} | ||
ControlFlow::CONTINUE | ||
} | ||
struct FoundParentLifetime; | ||
struct FindParentLifetimeVisitor<'tcx>(&'tcx ty::Generics); | ||
impl<'tcx> ty::fold::TypeVisitor<'tcx> for FindParentLifetimeVisitor<'tcx> { | ||
type BreakTy = FoundParentLifetime; | ||
|
||
fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> { | ||
debug!("check_opaque_for_inheriting_lifetimes: (visit_region) r={:?}", r); | ||
debug!("FindParentLifetimeVisitor: r={:?}", r); | ||
if let RegionKind::ReEarlyBound(ty::EarlyBoundRegion { index, .. }) = r { | ||
if *index < self.generics.parent_count as u32 { | ||
return ControlFlow::Break(None); | ||
if *index < self.0.parent_count as u32 { | ||
return ControlFlow::Break(FoundParentLifetime); | ||
} else { | ||
return ControlFlow::CONTINUE; | ||
} | ||
|
@@ -505,7 +491,7 @@ pub(super) fn check_opaque_for_inheriting_lifetimes( | |
|
||
fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> { | ||
if let ty::ConstKind::Unevaluated(..) = c.val { | ||
// FIXME(#72219) We currenctly don't detect lifetimes within substs | ||
// FIXME(#72219) We currently don't detect lifetimes within substs | ||
// which would violate this check. Even though the particular substitution is not used | ||
// within the const, this should still be fixed. | ||
return ControlFlow::CONTINUE; | ||
|
@@ -514,6 +500,26 @@ pub(super) fn check_opaque_for_inheriting_lifetimes( | |
} | ||
} | ||
|
||
#[derive(Debug)] | ||
struct ProhibitOpaqueVisitor<'tcx> { | ||
opaque_identity_ty: Ty<'tcx>, | ||
generics: &'tcx ty::Generics, | ||
} | ||
|
||
impl<'tcx> ty::fold::TypeVisitor<'tcx> for ProhibitOpaqueVisitor<'tcx> { | ||
type BreakTy = Ty<'tcx>; | ||
|
||
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> { | ||
debug!("check_opaque_for_inheriting_lifetimes: (visit_ty) t={:?}", t); | ||
if t == self.opaque_identity_ty { | ||
ControlFlow::CONTINUE | ||
} else { | ||
t.super_visit_with(&mut FindParentLifetimeVisitor(self.generics)) | ||
.map_break(|FoundParentLifetime| t) | ||
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. I think this in particular is pretty awesome ❤️ Modulo the fact that the visitor names start to look like Java classes now 😆 |
||
} | ||
} | ||
} | ||
|
||
if let ItemKind::OpaqueTy(hir::OpaqueTy { | ||
origin: hir::OpaqueTyOrigin::AsyncFn | hir::OpaqueTyOrigin::FnReturn, | ||
.. | ||
|
@@ -555,14 +561,12 @@ pub(super) fn check_opaque_for_inheriting_lifetimes( | |
|
||
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(span) { | ||
if snippet == "Self" { | ||
if let Some(ty) = ty { | ||
err.span_suggestion( | ||
span, | ||
"consider spelling out the type instead", | ||
format!("{:?}", ty), | ||
Applicability::MaybeIncorrect, | ||
); | ||
} | ||
err.span_suggestion( | ||
span, | ||
"consider spelling out the type instead", | ||
format!("{:?}", ty), | ||
Applicability::MaybeIncorrect, | ||
); | ||
} | ||
} | ||
err.emit(); | ||
|
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
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.