-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #87244 - jackh726:issue-71883, r=estebank
Better diagnostics with mismatched types due to implicit static lifetime Fixes #78113 I think this is my first diagnostics PR...definitely happy to hear thoughts on the direction/implementation here. I was originally just trying to solve the error above, where the lifetime on a GAT was causing a cryptic "mismatched types" error. But as I was writing this, I realized that this (unintentionally) also applied to a different case: `wf-in-foreign-fn-decls-issue-80468.rs`. I'm not sure if this diagnostic should get a new error code, or even reuse an existing one. And, there might be some ways to make this even more generalized. Also, the error is a bit more lengthy and verbose than probably needed. So thoughts there are welcome too. This PR essentially ended up adding a new nice region error pass that triggers if a type doesn't match the self type of an impl which is selected because of a predicate because of an implicit static bound on that self type. r? `@estebank`
- Loading branch information
Showing
16 changed files
with
299 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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 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
103 changes: 103 additions & 0 deletions
103
...ler/rustc_infer/src/infer/error_reporting/nice_region_error/mismatched_static_lifetime.rs
This file contains 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,103 @@ | ||
//! 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_data_structures::stable_set::FxHashSet; | ||
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) { | ||
// If an impl is local, then maybe this isn't what they want. Try to | ||
// be as helpful as possible with implicit lifetimes. | ||
|
||
// First, let's get the hir self type of the impl | ||
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."), | ||
}; | ||
|
||
// Next, let's figure out the set of trait objects with implict static bounds | ||
let ty = self.tcx().type_of(*impl_def_id); | ||
let mut v = super::static_impl_trait::TraitObjectVisitor(FxHashSet::default()); | ||
v.visit_ty(ty); | ||
let mut traits = vec![]; | ||
for matching_def_id in v.0 { | ||
let mut hir_v = | ||
super::static_impl_trait::HirTraitObjectVisitor(&mut traits, matching_def_id); | ||
hir_v.visit_ty(&impl_self_ty); | ||
} | ||
|
||
if traits.is_empty() { | ||
// If there are no trait object traits to point at, either because | ||
// there aren't trait objects or because none are implicit, then just | ||
// write a single note on the impl itself. | ||
|
||
let impl_span = self.tcx().def_span(*impl_def_id); | ||
err.span_note(impl_span, "...does not necessarily outlive the static lifetime introduced by the compatible `impl`"); | ||
} else { | ||
// Otherwise, point at all implicit static lifetimes | ||
|
||
err.note("...does not necessarily outlive the static lifetime introduced by the compatible `impl`"); | ||
for span in &traits { | ||
err.span_note(*span, "this has an implicit `'static` lifetime requirement"); | ||
// It would be nice to put this immediately under the above note, but they get | ||
// pushed to the end. | ||
err.span_suggestion_verbose( | ||
span.shrink_to_hi(), | ||
"consider relaxing the implicit `'static` requirement", | ||
" + '_".to_string(), | ||
Applicability::MaybeIncorrect, | ||
); | ||
} | ||
} | ||
} else { | ||
// Otherwise just point out the impl. | ||
|
||
let impl_span = self.tcx().def_span(*impl_def_id); | ||
err.span_note(impl_span, "...does not necessarily outlive the static lifetime introduced by the compatible `impl`"); | ||
} | ||
err.emit(); | ||
Some(ErrorReported) | ||
} | ||
} |
This file contains 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 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 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 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 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 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
40 changes: 40 additions & 0 deletions
40
src/test/ui/generic-associated-types/issue-78113-lifetime-mismatch-dyn-trait-box.rs
This file contains 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,40 @@ | ||
// 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 () { | ||
// `'a` doesn't match implicit `'static`: suggest `'_` | ||
type T<'a> = Box<dyn A + 'a>; //~ incompatible lifetime on type | ||
} | ||
|
||
trait C {} | ||
impl C for Box<dyn A + 'static> {} | ||
pub trait D { | ||
type T<'a>: C; | ||
} | ||
impl D for () { | ||
// `'a` doesn't match explicit `'static`: we *should* suggest removing `'static` | ||
type T<'a> = Box<dyn A + 'a>; //~ incompatible lifetime on type | ||
} | ||
|
||
trait E {} | ||
impl E for (Box<dyn A>, Box<dyn A>) {} | ||
pub trait F { | ||
type T<'a>: E; | ||
} | ||
impl F for () { | ||
// `'a` doesn't match explicit `'static`: suggest `'_` | ||
type T<'a> = (Box<dyn A + 'a>, Box<dyn A + 'a>); //~ incompatible lifetime on type | ||
} | ||
|
||
fn main() {} |
Oops, something went wrong.