-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Better diagnostics with mismatched types due to implicit static lifetime #87244
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
Changes from 1 commit
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
84 changes: 84 additions & 0 deletions
84
...ler/rustc_infer/src/infer/error_reporting/nice_region_error/mismatched_static_lifetime.rs
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
//! Error Reporting for when the lifetime for a type doesn't match the `impl` selected for a predicate | ||
//! to hold. | ||
|
||
use crate::infer::error_reporting::nice_region_error::NiceRegionError; | ||
use crate::infer::error_reporting::note_and_explain_region; | ||
use crate::infer::lexical_region_resolve::RegionResolutionError; | ||
use crate::infer::{SubregionOrigin, TypeTrace}; | ||
use crate::traits::ObligationCauseCode; | ||
use rustc_errors::{Applicability, ErrorReported}; | ||
use rustc_hir as hir; | ||
use rustc_hir::intravisit::Visitor; | ||
use rustc_middle::ty::{self, TypeVisitor}; | ||
use rustc_span::MultiSpan; | ||
|
||
impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { | ||
pub(super) fn try_report_mismatched_static_lifetime(&self) -> Option<ErrorReported> { | ||
let error = self.error.as_ref()?; | ||
debug!("try_report_mismatched_static_lifetime {:?}", error); | ||
|
||
let (origin, sub, sup) = match error.clone() { | ||
RegionResolutionError::ConcreteFailure(origin, sub, sup) => (origin, sub, sup), | ||
_ => return None, | ||
}; | ||
if *sub != ty::RegionKind::ReStatic { | ||
return None; | ||
} | ||
let cause = match origin { | ||
SubregionOrigin::Subtype(box TypeTrace { ref cause, .. }) => cause, | ||
_ => return None, | ||
}; | ||
let (parent, impl_def_id) = match &cause.code { | ||
ObligationCauseCode::MatchImpl(parent, impl_def_id) => (parent, impl_def_id), | ||
_ => return None, | ||
}; | ||
let binding_span = match **parent { | ||
ObligationCauseCode::BindingObligation(_def_id, binding_span) => binding_span, | ||
_ => return None, | ||
}; | ||
let mut err = self.tcx().sess.struct_span_err(cause.span, "incompatible lifetime on type"); | ||
// FIXME: we should point at the lifetime | ||
let mut multi_span: MultiSpan = vec![binding_span].into(); | ||
multi_span | ||
.push_span_label(binding_span, "introduces a `'static` lifetime requirement".into()); | ||
err.span_note(multi_span, "because this has an unmet lifetime requirement"); | ||
note_and_explain_region(self.tcx(), &mut err, "...", sup, "..."); | ||
if let Some(impl_node) = self.tcx().hir().get_if_local(*impl_def_id) { | ||
let ty = self.tcx().type_of(*impl_def_id); | ||
let mut v = super::static_impl_trait::TraitObjectVisitor(vec![]); | ||
v.visit_ty(ty); | ||
let matching_def_ids = v.0; | ||
|
||
let impl_self_ty = match impl_node { | ||
hir::Node::Item(hir::Item { | ||
kind: hir::ItemKind::Impl(hir::Impl { self_ty, .. }), | ||
.. | ||
}) => self_ty, | ||
_ => bug!("Node not an impl."), | ||
}; | ||
|
||
for matching_def_id in matching_def_ids { | ||
let mut hir_v = | ||
super::static_impl_trait::HirTraitObjectVisitor(vec![], matching_def_id); | ||
hir_v.visit_ty(&impl_self_ty); | ||
|
||
let mut multi_span: MultiSpan = hir_v.0.clone().into(); | ||
for span in &hir_v.0 { | ||
multi_span.push_span_label( | ||
*span, | ||
"this has an implicit `'static` lifetime requirement".to_string(), | ||
); | ||
err.span_suggestion_verbose( | ||
span.shrink_to_hi(), | ||
"consider relaxing the implicit `'static` requirement", | ||
" + '_".to_string(), | ||
Applicability::MaybeIncorrect, | ||
); | ||
} | ||
err.span_note(multi_span, "...does not necessarily outlive the static lifetime introduced by the compatible `impl`"); | ||
} | ||
jackh726 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
err.emit(); | ||
Some(ErrorReported) | ||
} | ||
} |
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
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
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
19 changes: 19 additions & 0 deletions
19
src/test/ui/generic-associated-types/issue-78113-lifetime-mismatch-dyn-trait-box.rs
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Test for diagnostics when we have mismatched lifetime due to implict 'static lifetime in GATs | ||
|
||
// check-fail | ||
|
||
#![feature(generic_associated_types)] | ||
|
||
pub trait A {} | ||
impl A for &dyn A {} | ||
impl A for Box<dyn A> {} | ||
|
||
pub trait B { | ||
type T<'a>: A; | ||
} | ||
|
||
impl B for () { | ||
type T<'a> = Box<dyn A + 'a>; //~ incompatible lifetime on type | ||
} | ||
|
||
fn main() {} |
28 changes: 28 additions & 0 deletions
28
src/test/ui/generic-associated-types/issue-78113-lifetime-mismatch-dyn-trait-box.stderr
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
error: incompatible lifetime on type | ||
--> $DIR/issue-78113-lifetime-mismatch-dyn-trait-box.rs:16:5 | ||
| | ||
LL | type T<'a> = Box<dyn A + 'a>; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: because this has an unmet lifetime requirement | ||
--> $DIR/issue-78113-lifetime-mismatch-dyn-trait-box.rs:12:17 | ||
| | ||
LL | type T<'a>: A; | ||
| ^ introduces a `'static` lifetime requirement | ||
note: ...the lifetime `'a` as defined on the associated item at 16:12... | ||
--> $DIR/issue-78113-lifetime-mismatch-dyn-trait-box.rs:16:12 | ||
| | ||
LL | type T<'a> = Box<dyn A + 'a>; | ||
| ^^ | ||
note: ...does not necessarily outlive the static lifetime introduced by the compatible `impl` | ||
--> $DIR/issue-78113-lifetime-mismatch-dyn-trait-box.rs:9:20 | ||
| | ||
LL | impl A for Box<dyn A> {} | ||
| ^ this has an implicit `'static` lifetime requirement | ||
help: consider relaxing the implicit `'static` requirement | ||
| | ||
LL | impl A for Box<dyn A + '_> {} | ||
| ^^^^ | ||
|
||
error: aborting due to previous error | ||
|
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
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.