Skip to content
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

Use full type name instead of just saying impl Trait in "captures lifetime" error #99748

Merged
merged 1 commit into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion compiler/rustc_borrowck/src/diagnostics/region_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ pub(crate) enum RegionErrorKind<'tcx> {
span: Span,
/// The hidden type.
hidden_ty: Ty<'tcx>,
/// The opaque type.
key: ty::OpaqueTypeKey<'tcx>,
/// The unexpected region.
member_region: ty::Region<'tcx>,
},
Expand Down Expand Up @@ -205,14 +207,16 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
}
}

RegionErrorKind::UnexpectedHiddenRegion { span, hidden_ty, member_region } => {
RegionErrorKind::UnexpectedHiddenRegion { span, hidden_ty, key, member_region } => {
let named_ty = self.regioncx.name_regions(self.infcx.tcx, hidden_ty);
let named_key = self.regioncx.name_regions(self.infcx.tcx, key);
let named_region = self.regioncx.name_regions(self.infcx.tcx, member_region);
self.buffer_error(unexpected_hidden_region_diagnostic(
self.infcx.tcx,
span,
named_ty,
named_region,
named_key,
));
}

Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_borrowck/src/member_constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub(crate) struct NllMemberConstraint<'tcx> {
/// The hidden type in which `R0` appears. (Used in error reporting.)
pub(crate) hidden_ty: Ty<'tcx>,

pub(crate) key: ty::OpaqueTypeKey<'tcx>,

/// The region `R0`.
pub(crate) member_region_vid: ty::RegionVid,

Expand Down Expand Up @@ -90,6 +92,7 @@ impl<'tcx> MemberConstraintSet<'tcx, ty::RegionVid> {
member_region_vid,
definition_span: m_c.definition_span,
hidden_ty: m_c.hidden_ty,
key: m_c.key,
start_index,
end_index,
});
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_borrowck/src/region_infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1763,6 +1763,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
errors_buffer.push(RegionErrorKind::UnexpectedHiddenRegion {
span: m_c.definition_span,
hidden_ty: m_c.hidden_ty,
key: m_c.key,
member_region,
});
}
Expand Down
13 changes: 7 additions & 6 deletions compiler/rustc_borrowck/src/region_infer/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
// after producing an error for each of them.
let definition_ty = instantiated_ty.ty.fold_with(&mut ReverseMapper::new(
self.tcx,
def_id,
opaque_type_key,
map,
instantiated_ty.ty,
instantiated_ty.span,
Expand Down Expand Up @@ -429,7 +429,7 @@ fn check_opaque_type_parameter_valid(
struct ReverseMapper<'tcx> {
tcx: TyCtxt<'tcx>,

opaque_type_def_id: LocalDefId,
key: ty::OpaqueTypeKey<'tcx>,
map: FxHashMap<GenericArg<'tcx>, GenericArg<'tcx>>,
map_missing_regions_to_empty: bool,

Expand All @@ -443,14 +443,14 @@ struct ReverseMapper<'tcx> {
impl<'tcx> ReverseMapper<'tcx> {
fn new(
tcx: TyCtxt<'tcx>,
opaque_type_def_id: LocalDefId,
key: ty::OpaqueTypeKey<'tcx>,
map: FxHashMap<GenericArg<'tcx>, GenericArg<'tcx>>,
hidden_ty: Ty<'tcx>,
span: Span,
) -> Self {
Self {
tcx,
opaque_type_def_id,
key,
map,
map_missing_regions_to_empty: false,
hidden_ty: Some(hidden_ty),
Expand Down Expand Up @@ -504,7 +504,7 @@ impl<'tcx> TypeFolder<'tcx> for ReverseMapper<'tcx> {
}
}

let generics = self.tcx().generics_of(self.opaque_type_def_id);
let generics = self.tcx().generics_of(self.key.def_id);
match self.map.get(&r.into()).map(|k| k.unpack()) {
Some(GenericArgKind::Lifetime(r1)) => r1,
Some(u) => panic!("region mapped to unexpected kind: {:?}", u),
Expand All @@ -513,9 +513,10 @@ impl<'tcx> TypeFolder<'tcx> for ReverseMapper<'tcx> {
if let Some(hidden_ty) = self.hidden_ty.take() {
unexpected_hidden_region_diagnostic(
self.tcx,
self.tcx.def_span(self.opaque_type_def_id),
self.tcx.def_span(self.key.def_id),
hidden_ty,
r,
self.key,
)
.emit();
}
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,14 @@ pub fn unexpected_hidden_region_diagnostic<'tcx>(
span: Span,
hidden_ty: Ty<'tcx>,
hidden_region: ty::Region<'tcx>,
opaque_ty: ty::OpaqueTypeKey<'tcx>,
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
let opaque_ty = tcx.mk_opaque(opaque_ty.def_id.to_def_id(), opaque_ty.substs);
let mut err = struct_span_err!(
tcx.sess,
span,
E0700,
"hidden type for `impl Trait` captures lifetime that does not appear in bounds",
"hidden type for `{opaque_ty}` captures lifetime that does not appear in bounds",
);

// Explain the region we are capturing.
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -974,14 +974,14 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
#[instrument(skip(self), level = "debug")]
pub fn member_constraint(
&self,
opaque_type_def_id: LocalDefId,
key: ty::OpaqueTypeKey<'tcx>,
definition_span: Span,
hidden_ty: Ty<'tcx>,
region: ty::Region<'tcx>,
in_regions: &Lrc<Vec<ty::Region<'tcx>>>,
) {
self.inner.borrow_mut().unwrap_region_constraints().member_constraint(
opaque_type_def_id,
key,
definition_span,
hidden_ty,
region,
Expand Down
10 changes: 1 addition & 9 deletions compiler/rustc_infer/src/infer/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,15 +394,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
);

concrete_ty.visit_with(&mut ConstrainOpaqueTypeRegionVisitor {
op: |r| {
self.member_constraint(
opaque_type_key.def_id,
span,
concrete_ty,
r,
&choice_regions,
)
},
op: |r| self.member_constraint(opaque_type_key, span, concrete_ty, r, &choice_regions),
});
}

Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_infer/src/infer/region_constraints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use rustc_data_structures::intern::Interned;
use rustc_data_structures::sync::Lrc;
use rustc_data_structures::undo_log::UndoLogs;
use rustc_data_structures::unify as ut;
use rustc_hir::def_id::LocalDefId;
use rustc_index::vec::IndexVec;
use rustc_middle::infer::unify_key::{RegionVidKey, UnifiedRegion};
use rustc_middle::ty::ReStatic;
Expand Down Expand Up @@ -533,7 +532,7 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {

pub fn member_constraint(
&mut self,
opaque_type_def_id: LocalDefId,
key: ty::OpaqueTypeKey<'tcx>,
definition_span: Span,
hidden_ty: Ty<'tcx>,
member_region: ty::Region<'tcx>,
Expand All @@ -546,7 +545,7 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
}

self.data.member_constraints.push(MemberConstraint {
opaque_type_def_id,
key,
definition_span,
hidden_ty,
member_region,
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_middle/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ pub mod canonical;
pub mod unify_key;

use crate::ty::Region;
use crate::ty::Ty;
use crate::ty::{OpaqueTypeKey, Ty};
use rustc_data_structures::sync::Lrc;
use rustc_hir::def_id::LocalDefId;
use rustc_span::Span;

/// Requires that `region` must be equal to one of the regions in `choice_regions`.
Expand All @@ -15,8 +14,9 @@ use rustc_span::Span;
/// ```
#[derive(Debug, Clone, HashStable, TypeFoldable, TypeVisitable, Lift)]
pub struct MemberConstraint<'tcx> {
/// The `DefId` of the opaque type causing this constraint: used for error reporting.
pub opaque_type_def_id: LocalDefId,
/// The `DefId` and substs of the opaque type causing this constraint.
/// Used for error reporting.
pub key: OpaqueTypeKey<'tcx>,

/// The span where the hidden type was instantiated.
pub definition_span: Span,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ LL | | }
|
= help: consider adding the following bound: `'a: 'b`

error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
error[E0700]: hidden type for `impl Trait<'a>` captures lifetime that does not appear in bounds
--> $DIR/ret-impl-trait-one.rs:16:80
|
LL | async fn async_ret_impl_trait1<'a, 'b>(a: &'a u8, b: &'b u8) -> impl Trait<'a> {
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/impl-trait/hidden-lifetimes.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
error[E0700]: hidden type for `impl Swap` captures lifetime that does not appear in bounds
--> $DIR/hidden-lifetimes.rs:29:5
|
LL | fn hide_ref<'a, 'b, T: 'static>(x: &'a mut &'b T) -> impl Swap + 'a {
Expand All @@ -11,7 +11,7 @@ help: to declare that the `impl Trait` captures `'b`, you can add an explicit `'
LL | fn hide_ref<'a, 'b, T: 'static>(x: &'a mut &'b T) -> impl Swap + 'a + 'b {
| ++++

error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
error[E0700]: hidden type for `impl Swap` captures lifetime that does not appear in bounds
--> $DIR/hidden-lifetimes.rs:46:5
|
LL | fn hide_rc_refcell<'a, 'b: 'a, T: 'static>(x: Rc<RefCell<&'b T>>) -> impl Swap + 'a {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn foo<'a: 'b, 'b, 'c>(x: &'static i32, mut y: &'a i32) -> E<'b, 'c> {
let _: &'b i32 = *u.0;
}
u.0
//~^ ERROR hidden type for `impl Trait` captures lifetime that does not appear in bounds
//~^ ERROR hidden type for `E<'b, 'c>` captures lifetime that does not appear in bounds
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
error[E0700]: hidden type for `E<'b, 'c>` captures lifetime that does not appear in bounds
--> $DIR/error-handling-2.rs:22:5
|
LL | fn foo<'a: 'b, 'b, 'c>(x: &'static i32, mut y: &'a i32) -> E<'b, 'c> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ where
// 'a in ['d, 'e]
// ```
if condition() { a } else { b }
//~^ ERROR hidden type for `impl Trait` captures lifetime that does not appear in bounds
//~^ ERROR hidden type for `impl Trait<'d, 'e>` captures lifetime that does not appear in bounds
}

fn condition() -> bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
error[E0700]: hidden type for `impl Trait<'d, 'e>` captures lifetime that does not appear in bounds
--> $DIR/ordinary-bounds-unrelated.rs:28:33
|
LL | fn upper_bounds<'a, 'b, 'c, 'd, 'e>(a: Ordinary<'a>, b: Ordinary<'b>) -> impl Trait<'d, 'e>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn upper_bounds<'a, 'b>(a: Ordinary<'a>, b: Ordinary<'b>) -> impl Trait<'a, 'b>
//
// We are forced to pick that '0 = 'e, because only 'e is outlived by *both* 'a and 'b.
if condition() { a } else { b }
//~^ ERROR hidden type for `impl Trait` captures lifetime that does not appear in bounds
//~^ ERROR hidden type for `impl Trait<'a, 'b>` captures lifetime that does not appear in bounds
}

fn condition() -> bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
error[E0700]: hidden type for `impl Trait<'a, 'b>` captures lifetime that does not appear in bounds
--> $DIR/ordinary-bounds-unsuited.rs:31:33
|
LL | fn upper_bounds<'a, 'b>(a: Ordinary<'a>, b: Ordinary<'b>) -> impl Trait<'a, 'b>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
error[E0700]: hidden type for `impl Copy` captures lifetime that does not appear in bounds
--> $DIR/must_outlive_least_region_or_bound.rs:3:35
|
LL | fn elided(x: &i32) -> impl Copy { x }
Expand All @@ -11,7 +11,7 @@ help: to declare that the `impl Trait` captures `'_`, you can add an explicit `'
LL | fn elided(x: &i32) -> impl Copy + '_ { x }
| ++++

error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
error[E0700]: hidden type for `impl Copy` captures lifetime that does not appear in bounds
--> $DIR/must_outlive_least_region_or_bound.rs:6:44
|
LL | fn explicit<'a>(x: &'a i32) -> impl Copy { x }
Expand Down Expand Up @@ -96,7 +96,7 @@ help: alternatively, add an explicit `'static` bound to this reference
LL | fn with_bound<'a>(x: &'static i32) -> impl LifetimeTrait<'a> + 'static { x }
| ~~~~~~~~~~~~

error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
error[E0700]: hidden type for `impl Fn(&'a u32)` captures lifetime that does not appear in bounds
--> $DIR/must_outlive_least_region_or_bound.rs:38:5
|
LL | fn move_lifetime_into_fn<'a, 'b>(x: &'a u32, y: &'b u32) -> impl Fn(&'a u32) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/impl-trait/region-escape-via-bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn foo<'x, 'y>(x: Cell<&'x u32>) -> impl Trait<'y>
where 'x: 'y
{
x
//~^ ERROR hidden type for `impl Trait` captures lifetime that does not appear in bounds [E0700]
//~^ ERROR hidden type for `impl Trait<'y>` captures lifetime that does not appear in bounds [E0700]
}

fn main() { }
2 changes: 1 addition & 1 deletion src/test/ui/impl-trait/region-escape-via-bound.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
error[E0700]: hidden type for `impl Trait<'y>` captures lifetime that does not appear in bounds
--> $DIR/region-escape-via-bound.rs:17:5
|
LL | fn foo<'x, 'y>(x: Cell<&'x u32>) -> impl Trait<'y>
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/impl-trait/static-return-lifetime-infered.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
error[E0700]: hidden type for `impl Iterator<Item = u32>` captures lifetime that does not appear in bounds
--> $DIR/static-return-lifetime-infered.rs:7:9
|
LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> {
Expand All @@ -11,7 +11,7 @@ help: to declare that the `impl Trait` captures `'_`, you can add an explicit `'
LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> + '_ {
| ++++

error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
error[E0700]: hidden type for `impl Iterator<Item = u32>` captures lifetime that does not appear in bounds
--> $DIR/static-return-lifetime-infered.rs:7:9
|
LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> {
Expand All @@ -24,7 +24,7 @@ help: to declare that the `impl Trait` captures `'_`, you can add an explicit `'
LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> + '_ {
| ++++

error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
error[E0700]: hidden type for `impl Iterator<Item = u32>` captures lifetime that does not appear in bounds
--> $DIR/static-return-lifetime-infered.rs:12:9
|
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> {
Expand All @@ -37,7 +37,7 @@ help: to declare that the `impl Trait` captures `'a`, you can add an explicit `'
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> + 'a {
| ++++

error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
error[E0700]: hidden type for `impl Iterator<Item = u32>` captures lifetime that does not appear in bounds
--> $DIR/static-return-lifetime-infered.rs:12:9
|
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> {
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/nll/issue-73159-rpit-static.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
error[E0700]: hidden type for `impl Iterator<Item = u8>` captures lifetime that does not appear in bounds
--> $DIR/issue-73159-rpit-static.rs:8:9
|
LL | impl<'a> Foo<'a> {
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/nll/ty-outlives/impl-trait-captures.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
error[E0700]: hidden type for `Opaque(DefId(0:11 ~ impl_trait_captures[1afc]::foo::{opaque#0}), [ReStatic, T, ReEarlyBound(0, 'a)])` captures lifetime that does not appear in bounds
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

side-note that this test uses -Zverbose, so not my fault and don't think it needs fixing.

--> $DIR/impl-trait-captures.rs:11:5
|
LL | fn foo<'a, T>(x: &T) -> impl Foo<'a> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
error[E0700]: hidden type for `impl Clone` captures lifetime that does not appear in bounds
--> $DIR/arbitrary_self_types_pin_lifetime_impl_trait-async.rs:8:48
|
LL | async fn f(self: Pin<&Self>) -> impl Clone { self }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
error[E0700]: hidden type for `impl Clone` captures lifetime that does not appear in bounds
--> $DIR/arbitrary_self_types_pin_lifetime_impl_trait.rs:6:44
|
LL | fn f(self: Pin<&Self>) -> impl Clone { self }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ where
G: Get<T>,
{
move || {
//~^ ERROR hidden type for `impl Trait` captures lifetime
//~^ ERROR hidden type for `impl FnOnce()` captures lifetime
*dest = g.get();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | fn baz<G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
| |
| help: consider introducing lifetime `'a` here: `'a,`

error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
error[E0700]: hidden type for `impl FnOnce()` captures lifetime that does not appear in bounds
--> $DIR/missing-lifetimes-in-signature.rs:19:5
|
LL | fn foo<G, T>(g: G, dest: &mut T) -> impl FnOnce()
Expand Down