1
Fork 0

Rollup merge of #99748 - compiler-errors:better-impl-trait-printing, r=fee1-dead

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

I think this is very useful, especially when there's >1 `impl Trait`, and it just means passing around a bit more info that we already have access to.
This commit is contained in:
Dylan DPC 2022-07-26 14:27:00 +05:30 committed by GitHub
commit ad32667bc8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 52 additions and 50 deletions

View file

@ -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>,
},
@ -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,
));
}

View file

@ -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,
@ -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,
});

View file

@ -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,
});
}

View file

@ -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,
@ -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,
@ -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),
@ -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),
@ -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();
}

View file

@ -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.

View file

@ -966,14 +966,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,

View file

@ -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),
});
}

View file

@ -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;
@ -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>,
@ -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,

View file

@ -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`.
@ -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,