1
Fork 0

Use ty::Instance::resolve to identify 'static bound source

This commit is contained in:
Esteban Küber 2020-06-30 17:41:15 -07:00
parent 7bf39fa9d9
commit f80743712e
10 changed files with 198 additions and 184 deletions

View file

@ -3,18 +3,20 @@
use crate::infer::error_reporting::nice_region_error::NiceRegionError; use crate::infer::error_reporting::nice_region_error::NiceRegionError;
use crate::infer::lexical_region_resolve::RegionResolutionError; use crate::infer::lexical_region_resolve::RegionResolutionError;
use crate::infer::{SubregionOrigin, TypeTrace}; use crate::infer::{SubregionOrigin, TypeTrace};
use crate::traits::ObligationCauseCode; use crate::traits::{ObligationCauseCode, UnifyReceiverContext};
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorReported}; use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorReported};
use rustc_hir::def_id::{DefId, LOCAL_CRATE}; use rustc_hir::def_id::DefId;
use rustc_hir::intravisit::{walk_ty, ErasedMap, NestedVisitorMap, Visitor}; use rustc_hir::intravisit::{walk_ty, ErasedMap, NestedVisitorMap, Visitor};
use rustc_hir::{self as hir, GenericBound, Item, ItemKind, Lifetime, LifetimeName, Node, TyKind}; use rustc_hir::{
use rustc_middle::ty::{ self as hir, GenericBound, ImplItem, Item, ItemKind, Lifetime, LifetimeName, Node, TraitItem,
self, AssocItem, AssocItemContainer, RegionKind, Ty, TypeFoldable, TypeVisitor, TyKind,
}; };
use rustc_middle::ty::{self, AssocItemContainer, RegionKind, Ty, TypeFoldable, TypeVisitor};
use rustc_span::{MultiSpan, Span}; use rustc_span::{MultiSpan, Span};
impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
/// Print the error message for lifetime errors when the return type is a static impl Trait. /// Print the error message for lifetime errors when the return type is a static `impl Trait`,
/// `dyn Trait` or if a method call on a trait object introduces a static requirement.
pub(super) fn try_report_static_impl_trait(&self) -> Option<ErrorReported> { pub(super) fn try_report_static_impl_trait(&self) -> Option<ErrorReported> {
debug!("try_report_static_impl_trait(error={:?})", self.error); debug!("try_report_static_impl_trait(error={:?})", self.error);
let tcx = self.tcx(); let tcx = self.tcx();
@ -34,8 +36,8 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
sub_r, sub_r,
sup_r, sup_r,
) if **sub_r == RegionKind::ReStatic => { ) if **sub_r == RegionKind::ReStatic => {
// This is for the implicit `'static` requirement coming from `impl dyn Trait {}`. // This is for an implicit `'static` requirement coming from `impl dyn Trait {}`.
if let ObligationCauseCode::UnifyReceiver(assoc) = &cause.code { if let ObligationCauseCode::UnifyReceiver(ctxt) = &cause.code {
let param = self.find_param_with_region(sup_r, sub_r)?; let param = self.find_param_with_region(sup_r, sub_r)?;
let lifetime = if sup_r.has_name() { let lifetime = if sup_r.has_name() {
format!("lifetime `{}`", sup_r) format!("lifetime `{}`", sup_r)
@ -55,7 +57,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
.map(|s| format!("`{}`", s)) .map(|s| format!("`{}`", s))
.unwrap_or_else(|| "`fn` parameter".to_string()), .unwrap_or_else(|| "`fn` parameter".to_string()),
lifetime, lifetime,
assoc.ident, ctxt.assoc_item.ident,
); );
err.span_label(param.param_ty_span, &format!("this data with {}...", lifetime)); err.span_label(param.param_ty_span, &format!("this data with {}...", lifetime));
err.span_label( err.span_label(
@ -63,7 +65,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
&format!( &format!(
"...is captured and required to live as long as `'static` here \ "...is captured and required to live as long as `'static` here \
because of an implicit lifetime bound on the {}", because of an implicit lifetime bound on the {}",
match assoc.container { match ctxt.assoc_item.container {
AssocItemContainer::TraitContainer(id) => AssocItemContainer::TraitContainer(id) =>
format!("`impl` of `{}`", tcx.def_path_str(id)), format!("`impl` of `{}`", tcx.def_path_str(id)),
AssocItemContainer::ImplContainer(_) => AssocItemContainer::ImplContainer(_) =>
@ -71,7 +73,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
}, },
), ),
); );
if self.find_impl_on_dyn_trait(&mut err, param.param_ty, assoc) { if self.find_impl_on_dyn_trait(&mut err, param.param_ty, &ctxt) {
err.emit(); err.emit();
return Some(ErrorReported); return Some(ErrorReported);
} else { } else {
@ -117,25 +119,26 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
let mut postfix = String::new(); let mut postfix = String::new();
if let SubregionOrigin::Subtype(box TypeTrace { cause, .. }) = &sup_origin { if let SubregionOrigin::Subtype(box TypeTrace { cause, .. }) = &sup_origin {
if let ObligationCauseCode::UnifyReceiver(assoc) = &cause.code { if let ObligationCauseCode::UnifyReceiver(ctxt) = &cause.code {
if self.find_impl_on_dyn_trait(&mut err, param.param_ty, assoc) if self.find_impl_on_dyn_trait(&mut err, param.param_ty, &ctxt)
&& fn_returns.is_empty() && fn_returns.is_empty()
{ {
err.code(rustc_errors::error_code!(E0767)); err.code(rustc_errors::error_code!(E0767));
err.set_primary_message(&format!( err.set_primary_message(&format!(
"{} has {} but calling `{}` introduces an implicit `'static` lifetime \ "{} has {} but calling `{}` introduces an implicit `'static` lifetime \
requirement", requirement",
param_name, lifetime, assoc.ident, param_name, lifetime, ctxt.assoc_item.ident,
)); ));
postfix = format!( postfix = format!(
" because of an implicit lifetime on the {}", " because of an implicit lifetime on the {}",
match assoc.container { match ctxt.assoc_item.container {
AssocItemContainer::TraitContainer(id) => AssocItemContainer::TraitContainer(id) =>
format!("`impl` of `{}`", tcx.def_path_str(id)), format!("`impl` of `{}`", tcx.def_path_str(id)),
AssocItemContainer::ImplContainer(_) => "inherent `impl`".to_string(), AssocItemContainer::ImplContainer(_) => "inherent `impl`".to_string(),
}, },
); );
} }
// }
} }
} }
@ -316,128 +319,104 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
} }
/// When we call a method coming from an `impl Foo for dyn Bar`, `dyn Bar` introduces a default /// When we call a method coming from an `impl Foo for dyn Bar`, `dyn Bar` introduces a default
/// `'static` obligation. Find `impl` blocks that are implemented /// `'static` obligation. Suggest relaxing that implicit bound.
fn find_impl_on_dyn_trait( fn find_impl_on_dyn_trait(
&self, &self,
err: &mut DiagnosticBuilder<'_>, err: &mut DiagnosticBuilder<'_>,
ty: Ty<'_>, ty: Ty<'_>,
assoc: &AssocItem, ctxt: &UnifyReceiverContext<'tcx>,
) -> bool { ) -> bool {
let tcx = self.tcx(); let tcx = self.tcx();
let mut suggested = false; let mut suggested = false;
// Find the trait object types in the argument. // Find the method being called.
let mut v = TraitObjectVisitor(vec![]); let instance = match ty::Instance::resolve(
v.visit_ty(ty); tcx,
ctxt.param_env,
let container_id = match assoc.container { ctxt.assoc_item.def_id,
// When the obligation comes from an `impl Foo for dyn Bar {}`, we self.infcx.resolve_vars_if_possible(&ctxt.substs),
// have the `DefId` of the `trait` itself, not the relevant `impl` ) {
// block. Because of this, we have to look at all the `trait`s Ok(Some(instance)) => instance,
// available, and filter out all that are not of `Foo` (this `def_id`) _ => return false,
// and not of `Bar` (the `filter_map` later in this method).
AssocItemContainer::TraitContainer(def_id) => def_id,
// When the obligation comes from an `impl dyn Trait {}`, we already
// have the `DefId` of the relevant `Item`, so we use it directly.
AssocItemContainer::ImplContainer(def_id) => {
if let Some(Node::Item(Item { kind: ItemKind::Impl { self_ty, .. }, .. })) =
tcx.hir().get_if_local(def_id)
{
for found_did in &v.0 {
let mut hir_v = HirTraitObjectVisitor(vec![], *found_did);
hir_v.visit_ty(self_ty);
if let [span] = &hir_v.0[..] {
let mut multi_span: MultiSpan = vec![*span].into();
multi_span.push_span_label(
*span,
"this has an implicit `'static` lifetime requirement".to_string(),
);
multi_span.push_span_label(
assoc.ident.span,
"`'static` requirement is introduced when calling this method"
.to_string(),
);
err.span_note(
multi_span,
&format!(
"`{}`'s inherent `impl` has a `'static` requirement",
tcx.def_path_str(*found_did),
),
);
err.span_suggestion_verbose(
span.shrink_to_hi(),
"consider relaxing the implicit `'static` requirement",
" + '_".to_string(),
Applicability::MaybeIncorrect,
);
suggested = true;
}
}
}
return suggested;
}
}; };
// Find all the `impl`s in the local scope that can be called on the type parameter. And // Get the `Ident` of the method being called and the corresponding `impl` (to point at
// retain all that are `impl`s of the trait that originated the `'static` obligation. // `Bar` in `impl Foo for dyn Bar {}` and the definition of the method being called).
// This doesn't find `impl dyn Trait { /**/ }`, but that case is handled above. let (ident, self_ty) = match tcx.hir().get_if_local(instance.def_id()) {
let impl_self_tys = tcx Some(Node::ImplItem(ImplItem { ident, hir_id, .. })) => {
.all_traits(LOCAL_CRATE) match tcx.hir().find(tcx.hir().get_parent_item(*hir_id)) {
.iter() Some(Node::Item(Item { kind: ItemKind::Impl { self_ty, .. }, .. })) => {
.flat_map(|trait_did| tcx.hir().trait_impls(*trait_did)) (ident, self_ty)
.filter_map(|impl_node| { }
let impl_did = tcx.hir().local_def_id(*impl_node); _ => return false,
match tcx.hir().get_if_local(impl_did.to_def_id()) { }
Some(Node::Item(Item { }
kind: ItemKind::Impl { self_ty, of_trait: Some(of_trait), items, .. }, Some(Node::TraitItem(TraitItem { ident, hir_id, .. })) => {
.. let parent_id = tcx.hir().get_parent_item(*hir_id);
})) if of_trait.trait_def_id() == Some(container_id) => Some(( match tcx.hir().find(parent_id) {
self_ty, Some(Node::Item(Item { kind: ItemKind::Trait(..), .. })) => {
// Get the ident of the method, in order to use its `Span`. // The method being called is defined in the `trait`, but the `'static`
items // obligation comes from the `impl`. Find that `impl` so that we can point
// at it in the suggestion.
let trait_did = tcx.hir().local_def_id(parent_id).to_def_id();
match tcx.hir().trait_impls(trait_did)
.iter() .iter()
.filter(|item| item.ident == assoc.ident) .filter_map(|impl_node| {
.map(|item| item.ident) let impl_did = tcx.hir().local_def_id(*impl_node);
match tcx.hir().get_if_local(impl_did.to_def_id()) {
Some(Node::Item(Item {
kind: ItemKind::Impl { self_ty, of_trait: Some(of_trait), .. },
..
})) if of_trait.trait_def_id() == Some(trait_did) => Some(self_ty),
_ => None,
}
})
.next() .next()
.unwrap_or(assoc.ident), {
)), Some(self_ty) => (ident, self_ty),
_ => None, _ => return false,
}
}
_ => return false,
} }
}); }
_ => return false,
};
// Given all the `impl`s of the relevant `trait`, look for those that are implemented for // Find the trait object types in the argument, so we point at *only* the trait object.
// the trait object in the `fn` parameter type. let mut v = TraitObjectVisitor(vec![]);
for (self_ty, method) in impl_self_tys { v.visit_ty(ty);
for found_did in &v.0 { for found_did in &v.0 {
let mut hir_v = HirTraitObjectVisitor(vec![], *found_did); let mut hir_v = HirTraitObjectVisitor(vec![], *found_did);
hir_v.visit_ty(self_ty); hir_v.visit_ty(self_ty);
if let [span] = &hir_v.0[..] { for span in &hir_v.0 {
let mut multi_span: MultiSpan = vec![*span].into(); let mut multi_span: MultiSpan = vec![*span].into();
multi_span.push_span_label( multi_span.push_span_label(
*span, *span,
"this has an implicit `'static` lifetime requirement".to_string(), "this has an implicit `'static` lifetime requirement".to_string(),
); );
multi_span.push_span_label( multi_span.push_span_label(
method.span, ident.span,
"`'static` requirement is introduced when calling this method".to_string(), "calling this method introduces the `impl`'s 'static` requirement".to_string(),
); );
err.span_note( err.span_note(
multi_span, multi_span,
&format!( &format!(
"`{}`'s `impl` of `{}` has an implicit `'static` requirement", "{} has a `'static` requirement",
tcx.def_path_str(*found_did), match ctxt.assoc_item.container {
tcx.def_path_str(container_id), AssocItemContainer::TraitContainer(id) =>
), format!("`impl` of `{}`", tcx.def_path_str(id)),
); AssocItemContainer::ImplContainer(_) => "inherent `impl`".to_string(),
err.span_suggestion_verbose( },
span.shrink_to_hi(), ),
"consider relaxing the implicit `'static` requirement", );
" + '_".to_string(), err.span_suggestion_verbose(
Applicability::MaybeIncorrect, span.shrink_to_hi(),
); "consider relaxing the implicit `'static` requirement",
suggested = true; " + '_".to_string(),
} Applicability::MaybeIncorrect,
);
suggested = true;
} }
} }
suggested suggested

View file

@ -169,6 +169,13 @@ impl<'tcx> ObligationCause<'tcx> {
} }
} }
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct UnifyReceiverContext<'tcx> {
pub assoc_item: ty::AssocItem,
pub param_env: ty::ParamEnv<'tcx>,
pub substs: SubstsRef<'tcx>,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum ObligationCauseCode<'tcx> { pub enum ObligationCauseCode<'tcx> {
/// Not well classified or should be obvious from the span. /// Not well classified or should be obvious from the span.
@ -300,7 +307,7 @@ pub enum ObligationCauseCode<'tcx> {
/// Method receiver /// Method receiver
MethodReceiver, MethodReceiver,
UnifyReceiver(Rc<ty::AssocItem>), UnifyReceiver(Box<UnifyReceiverContext<'tcx>>),
/// `return` with no expression /// `return` with no expression
ReturnNoExpression, ReturnNoExpression,

View file

@ -213,13 +213,26 @@ impl<'a, 'tcx> Lift<'tcx> for traits::ObligationCauseCode<'a> {
super::StartFunctionType => Some(super::StartFunctionType), super::StartFunctionType => Some(super::StartFunctionType),
super::IntrinsicType => Some(super::IntrinsicType), super::IntrinsicType => Some(super::IntrinsicType),
super::MethodReceiver => Some(super::MethodReceiver), super::MethodReceiver => Some(super::MethodReceiver),
super::UnifyReceiver(ref assoc) => Some(super::UnifyReceiver(assoc.clone())), super::UnifyReceiver(ref ctxt) => tcx.lift(ctxt).map(|ctxt| super::UnifyReceiver(ctxt)),
super::BlockTailExpression(id) => Some(super::BlockTailExpression(id)), super::BlockTailExpression(id) => Some(super::BlockTailExpression(id)),
super::TrivialBound => Some(super::TrivialBound), super::TrivialBound => Some(super::TrivialBound),
} }
} }
} }
impl<'a, 'tcx> Lift<'tcx> for traits::UnifyReceiverContext<'a> {
type Lifted = traits::UnifyReceiverContext<'tcx>;
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
tcx.lift(&self.param_env).and_then(|param_env| {
tcx.lift(&self.substs).map(|substs| traits::UnifyReceiverContext {
assoc_item: self.assoc_item,
param_env,
substs,
})
})
}
}
impl<'a, 'tcx> Lift<'tcx> for traits::DerivedObligationCause<'a> { impl<'a, 'tcx> Lift<'tcx> for traits::DerivedObligationCause<'a> {
type Lifted = traits::DerivedObligationCause<'tcx>; type Lifted = traits::DerivedObligationCause<'tcx>;
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> { fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {

View file

@ -6,6 +6,7 @@
use crate::infer::{InferCtxt, TyCtxtInferExt}; use crate::infer::{InferCtxt, TyCtxtInferExt};
use crate::traits::{ use crate::traits::{
FulfillmentContext, ImplSource, Obligation, ObligationCause, SelectionContext, TraitEngine, FulfillmentContext, ImplSource, Obligation, ObligationCause, SelectionContext, TraitEngine,
Unimplemented,
}; };
use rustc_errors::ErrorReported; use rustc_errors::ErrorReported;
use rustc_middle::ty::fold::TypeFoldable; use rustc_middle::ty::fold::TypeFoldable;
@ -58,6 +59,18 @@ pub fn codegen_fulfill_obligation<'tcx>(
); );
return Err(ErrorReported); return Err(ErrorReported);
} }
Err(Unimplemented) => {
// This can trigger when we probe for the source of a `'static` lifetime requirement
// on a trait object: `impl Foo for dyn Trait {}` has an implicit `'static` bound.
infcx.tcx.sess.delay_span_bug(
rustc_span::DUMMY_SP,
&format!(
"Encountered error `Unimplemented` selecting `{:?}` during codegen",
trait_ref
),
);
return Err(ErrorReported);
}
Err(e) => { Err(e) => {
bug!("Encountered error `{:?}` selecting `{:?}` during codegen", e, trait_ref) bug!("Encountered error `{:?}` selecting `{:?}` during codegen", e, trait_ref)
} }

View file

@ -1706,7 +1706,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
| ObligationCauseCode::IntrinsicType | ObligationCauseCode::IntrinsicType
| ObligationCauseCode::MethodReceiver | ObligationCauseCode::MethodReceiver
| ObligationCauseCode::ReturnNoExpression | ObligationCauseCode::ReturnNoExpression
| ObligationCauseCode::UnifyReceiver(_) | ObligationCauseCode::UnifyReceiver(..)
| ObligationCauseCode::MiscObligation => {} | ObligationCauseCode::MiscObligation => {}
ObligationCauseCode::SliceOrArrayElem => { ObligationCauseCode::SliceOrArrayElem => {
err.note("slice and array elements must have `Sized` type"); err.note("slice and array elements must have `Sized` type");

View file

@ -6,7 +6,7 @@ use crate::hir::def_id::DefId;
use crate::hir::GenericArg; use crate::hir::GenericArg;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_infer::infer::{self, InferOk}; use rustc_infer::infer::{self, InferOk};
use rustc_middle::traits::ObligationCauseCode; use rustc_middle::traits::{ObligationCauseCode, UnifyReceiverContext};
use rustc_middle::ty::adjustment::{Adjust, Adjustment, PointerCast}; use rustc_middle::ty::adjustment::{Adjust, Adjustment, PointerCast};
use rustc_middle::ty::adjustment::{AllowTwoPhase, AutoBorrow, AutoBorrowMutability}; use rustc_middle::ty::adjustment::{AllowTwoPhase, AutoBorrow, AutoBorrowMutability};
use rustc_middle::ty::fold::TypeFoldable; use rustc_middle::ty::fold::TypeFoldable;
@ -16,7 +16,6 @@ use rustc_span::Span;
use rustc_trait_selection::traits; use rustc_trait_selection::traits;
use std::ops::Deref; use std::ops::Deref;
use std::rc::Rc;
struct ConfirmContext<'a, 'tcx> { struct ConfirmContext<'a, 'tcx> {
fcx: &'a FnCtxt<'a, 'tcx>, fcx: &'a FnCtxt<'a, 'tcx>,
@ -97,7 +96,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
"confirm: self_ty={:?} method_sig_rcvr={:?} method_sig={:?} method_predicates={:?}", "confirm: self_ty={:?} method_sig_rcvr={:?} method_sig={:?} method_predicates={:?}",
self_ty, method_sig_rcvr, method_sig, method_predicates self_ty, method_sig_rcvr, method_sig, method_predicates
); );
self.unify_receivers(self_ty, method_sig_rcvr, &pick); self.unify_receivers(self_ty, method_sig_rcvr, &pick, all_substs);
let (method_sig, method_predicates) = let (method_sig, method_predicates) =
self.normalize_associated_types_in(self.span, &(method_sig, method_predicates)); self.normalize_associated_types_in(self.span, &(method_sig, method_predicates));
@ -345,12 +344,20 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
self_ty: Ty<'tcx>, self_ty: Ty<'tcx>,
method_self_ty: Ty<'tcx>, method_self_ty: Ty<'tcx>,
pick: &probe::Pick<'tcx>, pick: &probe::Pick<'tcx>,
substs: SubstsRef<'tcx>,
) { ) {
debug!( debug!(
"unify_receivers: self_ty={:?} method_self_ty={:?} span={:?} pick={:?}", "unify_receivers: self_ty={:?} method_self_ty={:?} span={:?} pick={:?}",
self_ty, method_self_ty, self.span, pick self_ty, method_self_ty, self.span, pick
); );
let cause = self.cause(self.span, ObligationCauseCode::UnifyReceiver(Rc::new(pick.item))); let cause = self.cause(
self.span,
ObligationCauseCode::UnifyReceiver(Box::new(UnifyReceiverContext {
assoc_item: pick.item,
param_env: self.param_env,
substs,
})),
);
match self.at(&cause, self.param_env).sup(method_self_ty, self_ty) { match self.at(&cause, self.param_env).sup(method_self_ty, self_ty) {
Ok(InferOk { obligations, value: () }) => { Ok(InferOk { obligations, value: () }) => {
self.register_predicates(obligations); self.register_predicates(obligations);

View file

@ -5,19 +5,19 @@ mod foo {
trait OtherTrait<'a> {} trait OtherTrait<'a> {}
impl<'a> OtherTrait<'a> for &'a () {} impl<'a> OtherTrait<'a> for &'a () {}
trait ObjectTrait {} trait ObjectTrait<T> {}
trait MyTrait { trait MyTrait<T> {
fn use_self(&self) -> &(); fn use_self<K>(&self) -> &();
} }
trait Irrelevant {} trait Irrelevant {}
impl MyTrait for dyn ObjectTrait + '_ { impl<T> MyTrait<T> for dyn ObjectTrait<T> + '_ {
fn use_self(&self) -> &() { panic!() } fn use_self<K>(&self) -> &() { panic!() }
} }
impl Irrelevant for dyn ObjectTrait {} impl<T> Irrelevant for dyn ObjectTrait<T> {}
fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a { fn use_it<'a, T>(val: &'a dyn ObjectTrait<T>) -> impl OtherTrait<'a> + 'a {
val.use_self() //~ ERROR E0759 val.use_self::<T>() //~ ERROR E0759
} }
} }
@ -76,13 +76,11 @@ mod ban {
trait ObjectTrait {} trait ObjectTrait {}
trait MyTrait { trait MyTrait {
fn use_self(&self) -> &(); fn use_self(&self) -> &() { panic!() }
} }
trait Irrelevant {} trait Irrelevant {}
impl MyTrait for dyn ObjectTrait + '_ { impl MyTrait for dyn ObjectTrait + '_ {}
fn use_self(&self) -> &() { panic!() }
}
impl Irrelevant for dyn ObjectTrait {} impl Irrelevant for dyn ObjectTrait {}
fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a { fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a {
@ -90,5 +88,4 @@ mod ban {
} }
} }
fn main() {} fn main() {}

View file

@ -1,10 +1,10 @@
error[E0521]: borrowed data escapes outside of function error[E0521]: borrowed data escapes outside of function
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:20:9 --> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:20:9
| |
LL | fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a { LL | fn use_it<'a, T>(val: &'a dyn ObjectTrait<T>) -> impl OtherTrait<'a> + 'a {
| --- `val` is a reference that is only valid in the function body | --- `val` is a reference that is only valid in the function body
LL | val.use_self() LL | val.use_self::<T>()
| ^^^^^^^^^^^^^^ `val` escapes the function body here | ^^^^^^^^^^^^^^^^^^^ `val` escapes the function body here
| |
= help: consider replacing `'a` with `'static` = help: consider replacing `'a` with `'static`
@ -19,7 +19,7 @@ LL | val.use_self()
= help: consider replacing `'a` with `'static` = help: consider replacing `'a` with `'static`
error[E0521]: borrowed data escapes outside of function error[E0521]: borrowed data escapes outside of function
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:89:9 --> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:87:9
| |
LL | fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> { LL | fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> {
| --- `val` is a reference that is only valid in the function body | --- `val` is a reference that is only valid in the function body

View file

@ -5,19 +5,19 @@ mod foo {
trait OtherTrait<'a> {} trait OtherTrait<'a> {}
impl<'a> OtherTrait<'a> for &'a () {} impl<'a> OtherTrait<'a> for &'a () {}
trait ObjectTrait {} trait ObjectTrait<T> {}
trait MyTrait { trait MyTrait<T> {
fn use_self(&self) -> &(); fn use_self<K>(&self) -> &();
} }
trait Irrelevant {} trait Irrelevant {}
impl MyTrait for dyn ObjectTrait { impl<T> MyTrait<T> for dyn ObjectTrait<T> {
fn use_self(&self) -> &() { panic!() } fn use_self<K>(&self) -> &() { panic!() }
} }
impl Irrelevant for dyn ObjectTrait {} impl<T> Irrelevant for dyn ObjectTrait<T> {}
fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a { fn use_it<'a, T>(val: &'a dyn ObjectTrait<T>) -> impl OtherTrait<'a> + 'a {
val.use_self() //~ ERROR E0759 val.use_self::<T>() //~ ERROR E0759
} }
} }
@ -76,13 +76,11 @@ mod ban {
trait ObjectTrait {} trait ObjectTrait {}
trait MyTrait { trait MyTrait {
fn use_self(&self) -> &(); fn use_self(&self) -> &() { panic!() }
} }
trait Irrelevant {} trait Irrelevant {}
impl MyTrait for dyn ObjectTrait { impl MyTrait for dyn ObjectTrait {}
fn use_self(&self) -> &() { panic!() }
}
impl Irrelevant for dyn ObjectTrait {} impl Irrelevant for dyn ObjectTrait {}
fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> { fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> {
@ -90,5 +88,4 @@ mod ban {
} }
} }
fn main() {} fn main() {}

View file

@ -1,22 +1,22 @@
error[E0759]: `val` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement error[E0759]: `val` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:20:13 --> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:20:13
| |
LL | fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a { LL | fn use_it<'a, T>(val: &'a dyn ObjectTrait<T>) -> impl OtherTrait<'a> + 'a {
| ------------------- this data with lifetime `'a`... | ---------------------- this data with lifetime `'a`...
LL | val.use_self() LL | val.use_self::<T>()
| ^^^^^^^^ ...is captured and required to live as long as `'static` here | ^^^^^^^^ ...is captured and required to live as long as `'static` here
| |
note: `foo::ObjectTrait`'s `impl` of `foo::MyTrait` has an implicit `'static` requirement note: `impl` of `foo::MyTrait` has a `'static` requirement
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:14:26 --> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:14:32
| |
LL | impl MyTrait for dyn ObjectTrait { LL | impl<T> MyTrait<T> for dyn ObjectTrait<T> {
| ^^^^^^^^^^^ this has an implicit `'static` lifetime requirement | ^^^^^^^^^^^^^^ this has an implicit `'static` lifetime requirement
LL | fn use_self(&self) -> &() { panic!() } LL | fn use_self<K>(&self) -> &() { panic!() }
| -------- `'static` requirement is introduced when calling this method | -------- calling this method introduces the `impl`'s 'static` requirement
help: consider relaxing the implicit `'static` requirement help: consider relaxing the implicit `'static` requirement
| |
LL | impl MyTrait for dyn ObjectTrait + '_ { LL | impl<T> MyTrait<T> for dyn ObjectTrait<T> + '_ {
| ^^^^ | ^^^^
error[E0767]: `val` has lifetime `'a` but calling `use_self` introduces an implicit `'static` lifetime requirement error[E0767]: `val` has lifetime `'a` but calling `use_self` introduces an implicit `'static` lifetime requirement
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:69:13 --> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:69:13
@ -26,36 +26,37 @@ LL | fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a {
LL | val.use_self() LL | val.use_self()
| ^^^^^^^^ ...is captured and required to live as long as `'static` here because of an implicit lifetime bound on the inherent `impl` | ^^^^^^^^ ...is captured and required to live as long as `'static` here because of an implicit lifetime bound on the inherent `impl`
| |
note: `bat::ObjectTrait`'s inherent `impl` has a `'static` requirement note: inherent `impl` has a `'static` requirement
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:64:14 --> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:64:14
| |
LL | impl dyn ObjectTrait { LL | impl dyn ObjectTrait {
| ^^^^^^^^^^^ this has an implicit `'static` lifetime requirement | ^^^^^^^^^^^ this has an implicit `'static` lifetime requirement
LL | fn use_self(&self) -> &() { panic!() } LL | fn use_self(&self) -> &() { panic!() }
| -------- `'static` requirement is introduced when calling this method | -------- calling this method introduces the `impl`'s 'static` requirement
help: consider relaxing the implicit `'static` requirement help: consider relaxing the implicit `'static` requirement
| |
LL | impl dyn ObjectTrait + '_ { LL | impl dyn ObjectTrait + '_ {
| ^^^^ | ^^^^
error[E0759]: `val` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement error[E0759]: `val` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:89:13 --> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:87:13
| |
LL | fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> { LL | fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> {
| ------------------- this data with lifetime `'a`... | ------------------- this data with lifetime `'a`...
LL | val.use_self() LL | val.use_self()
| ^^^^^^^^ ...is captured and required to live as long as `'static` here | ^^^^^^^^ ...is captured and required to live as long as `'static` here
| |
note: `ban::ObjectTrait`'s `impl` of `ban::MyTrait` has an implicit `'static` requirement note: `impl` of `ban::MyTrait` has a `'static` requirement
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:83:26 --> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:83:26
| |
LL | impl MyTrait for dyn ObjectTrait {
| ^^^^^^^^^^^ this has an implicit `'static` lifetime requirement
LL | fn use_self(&self) -> &() { panic!() } LL | fn use_self(&self) -> &() { panic!() }
| -------- `'static` requirement is introduced when calling this method | -------- calling this method introduces the `impl`'s 'static` requirement
...
LL | impl MyTrait for dyn ObjectTrait {}
| ^^^^^^^^^^^ this has an implicit `'static` lifetime requirement
help: consider relaxing the implicit `'static` requirement help: consider relaxing the implicit `'static` requirement
| |
LL | impl MyTrait for dyn ObjectTrait + '_ { LL | impl MyTrait for dyn ObjectTrait + '_ {}
| ^^^^ | ^^^^
help: to declare that the `impl Trait` captures data from argument `val`, you can add an explicit `'a` lifetime bound help: to declare that the `impl Trait` captures data from argument `val`, you can add an explicit `'a` lifetime bound
| |
@ -70,13 +71,13 @@ LL | fn use_it<'a>(val: &'a dyn ObjectTrait) -> &'a () {
LL | val.use_self() LL | val.use_self()
| ^^^^^^^^ ...is captured and required to live as long as `'static` here because of an implicit lifetime on the `impl` of `bar::MyTrait` | ^^^^^^^^ ...is captured and required to live as long as `'static` here because of an implicit lifetime on the `impl` of `bar::MyTrait`
| |
note: `bar::ObjectTrait`'s `impl` of `bar::MyTrait` has an implicit `'static` requirement note: `impl` of `bar::MyTrait` has a `'static` requirement
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:31:26 --> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:31:26
| |
LL | impl MyTrait for dyn ObjectTrait { LL | impl MyTrait for dyn ObjectTrait {
| ^^^^^^^^^^^ this has an implicit `'static` lifetime requirement | ^^^^^^^^^^^ this has an implicit `'static` lifetime requirement
LL | fn use_self(&self) -> &() { panic!() } LL | fn use_self(&self) -> &() { panic!() }
| -------- `'static` requirement is introduced when calling this method | -------- calling this method introduces the `impl`'s 'static` requirement
help: consider relaxing the implicit `'static` requirement help: consider relaxing the implicit `'static` requirement
| |
LL | impl MyTrait for dyn ObjectTrait + '_ { LL | impl MyTrait for dyn ObjectTrait + '_ {
@ -90,13 +91,13 @@ LL | fn use_it<'a>(val: &'a Box<dyn ObjectTrait + 'a>) -> &'a () {
LL | val.use_self() LL | val.use_self()
| ^^^^^^^^ ...is captured and required to live as long as `'static` here because of an implicit lifetime on the `impl` of `baz::MyTrait` | ^^^^^^^^ ...is captured and required to live as long as `'static` here because of an implicit lifetime on the `impl` of `baz::MyTrait`
| |
note: `baz::ObjectTrait`'s `impl` of `baz::MyTrait` has an implicit `'static` requirement note: `impl` of `baz::MyTrait` has a `'static` requirement
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:48:30 --> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:48:30
| |
LL | impl MyTrait for Box<dyn ObjectTrait> { LL | impl MyTrait for Box<dyn ObjectTrait> {
| ^^^^^^^^^^^ this has an implicit `'static` lifetime requirement | ^^^^^^^^^^^ this has an implicit `'static` lifetime requirement
LL | fn use_self(&self) -> &() { panic!() } LL | fn use_self(&self) -> &() { panic!() }
| -------- `'static` requirement is introduced when calling this method | -------- calling this method introduces the `impl`'s 'static` requirement
help: consider relaxing the implicit `'static` requirement help: consider relaxing the implicit `'static` requirement
| |
LL | impl MyTrait for Box<dyn ObjectTrait + '_> { LL | impl MyTrait for Box<dyn ObjectTrait + '_> {