-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Improve error message for lifetime error with dyn Trait
#67378
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
Closed
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
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
140 changes: 140 additions & 0 deletions
140
src/librustc/infer/error_reporting/nice_region_error/static_dyn_trait.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,140 @@ | ||
#![allow(unused)] | ||
//! Error Reporting for dyn Traits. | ||
use crate::infer::error_reporting::nice_region_error::NiceRegionError; | ||
use crate::infer::lexical_region_resolve::RegionResolutionError; | ||
use crate::ty::{self, BoundRegion, FreeRegion, RegionKind, DefIdTree, ParamEnv}; | ||
use crate::util::common::ErrorReported; | ||
use errors::Applicability; | ||
use crate::infer::{SubregionOrigin, ValuePairs, TypeTrace}; | ||
use crate::ty::error::ExpectedFound; | ||
use crate::hir; | ||
use crate::hir::def_id::DefId; | ||
use crate::traits::ObligationCauseCode::ExprAssignable; | ||
use crate::traits::ObligationCause; | ||
use crate::ty::{TyCtxt, TypeFoldable}; | ||
use crate::ty::subst::{Subst, InternalSubsts, SubstsRef}; | ||
use syntax_pos::Span; | ||
|
||
impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { | ||
/// Print the error message for lifetime errors when a static ref to a trait object is required | ||
/// because of a `dyn Trait` impl. | ||
pub(super) fn try_report_static_dyn_trait(&self) -> Option<ErrorReported> { | ||
let (span, sub, sup) = self.regions(); | ||
|
||
debug!( | ||
"try_report_static_dyn_trait: sub={:?}, sup={:?}, error={:?}", | ||
sub, | ||
sup, | ||
self.error, | ||
); | ||
|
||
if let Some(ref error) = self.error { | ||
let (found, origin_span) = match error { | ||
RegionResolutionError::ConcreteFailure(SubregionOrigin::Subtype(box TypeTrace { | ||
cause, | ||
values: ValuePairs::TraitRefs(ExpectedFound { expected, found }), | ||
}), _, _) => { | ||
(found, cause.span) | ||
}, | ||
// FIXME there is also the other region origin! | ||
RegionResolutionError::SubSupConflict(_, _, SubregionOrigin::Subtype(box TypeTrace { | ||
cause, | ||
values: ValuePairs::TraitRefs(ExpectedFound { expected, found }), | ||
}), _, _, _) => { | ||
(found, cause.span) | ||
} | ||
_ => { | ||
return None; | ||
} | ||
}; | ||
|
||
debug!( | ||
"try_report_static_dyn_trait: found={:?} origin_span={:?}", | ||
found, origin_span, | ||
); | ||
|
||
let found = self.infcx.resolve_vars_if_possible(found); | ||
let dyn_static_ty = found.self_ty().walk().find(|ty| | ||
if let ty::Dynamic(_, _) = ty.kind { true } else { false }); | ||
|
||
debug!( | ||
"try_report_static_dyn_trait: dyn_static_ty={:?} dyn_static_ty.kind={:?}", | ||
dyn_static_ty, dyn_static_ty.map(|dyn_static_ty| &dyn_static_ty.kind), | ||
); | ||
|
||
let dyn_trait_name = if let Some(dyn_static_ty) = dyn_static_ty { | ||
if let ty::Dynamic(binder, _) = dyn_static_ty.kind { | ||
binder.skip_binder().to_string() | ||
} else { | ||
return None; | ||
} | ||
} else { | ||
return None; | ||
}; | ||
|
||
let mut trait_impl = None; | ||
|
||
self.tcx().for_each_relevant_impl( | ||
found.def_id, | ||
found.self_ty(), | ||
|impl_def_id| { | ||
debug!( | ||
"try_report_static_dyn_trait: for_each_relevant_impl impl_def_id={:?}", | ||
impl_def_id, | ||
); | ||
|
||
trait_impl = Some(impl_def_id); | ||
}); | ||
|
||
debug!( | ||
"try_report_static_dyn_trait: trait_impl={:?}", trait_impl, | ||
); | ||
|
||
if let Some(impl_def_id) = trait_impl { | ||
self.emit_dyn_trait_err(origin_span, &dyn_trait_name, impl_def_id); | ||
return Some(ErrorReported); | ||
} | ||
} | ||
None | ||
} | ||
|
||
fn emit_dyn_trait_err(&self, | ||
expr_span: Span, | ||
dyn_trait_name: &String, | ||
impl_def_id: DefId, | ||
) { | ||
let (_, sub, sup) = self.regions(); | ||
|
||
debug!( | ||
"emit_dyn_trait_err: sup={:?} sub={:?} expr_span={:?} dyn_trait_name={:?}", | ||
sup, sub, expr_span, dyn_trait_name, | ||
); | ||
|
||
let item_span = self.tcx().sess.source_map() | ||
.def_span(self.tcx().def_span(impl_def_id)); | ||
|
||
let (lifetime_description, lt_sp_opt) = self.tcx().msg_span_from_free_region(sup); | ||
|
||
let impl_span = item_span; | ||
|
||
let mut err = self.tcx().sess.struct_span_err( | ||
expr_span, | ||
"cannot infer an appropriate lifetime", | ||
); | ||
|
||
if let Some(lt_sp_opt) = lt_sp_opt { | ||
err.span_note( | ||
lt_sp_opt, | ||
&format!("first, the lifetime cannot outlive {}...", lifetime_description), | ||
); | ||
} | ||
|
||
err.span_note(expr_span, | ||
&format!("but, the lifetime must be valid for the {} lifetime...", sub)); | ||
|
||
|
||
self.infcx.note_dyn_impl_and_suggest_anon_lifetime(&mut err, impl_def_id, dyn_trait_name); | ||
|
||
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have time to do an in-depth review right now, but we should try to come up with a different way of doing this. Fiddling with the textual representation of the code is a recipe for pain later on. It produces output that isn't always correct (like the problem with the missing parens and some incorrect suggestions I noticed below), and is brittle and affected by whitespace. I'll take another look later to see if I can have more actionable feedback on what to do instead.