Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d5b6510

Browse files
committed
Have the spans of TAIT type conflict errors point to the actual site instead of the owning function
1 parent 4b249b0 commit d5b6510

37 files changed

+180
-187
lines changed

compiler/rustc_borrowck/src/nll.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_middle::mir::{
88
BasicBlock, Body, ClosureOutlivesSubject, ClosureRegionRequirements, LocalKind, Location,
99
Promoted,
1010
};
11-
use rustc_middle::ty::{self, OpaqueTypeKey, Region, RegionVid, Ty};
11+
use rustc_middle::ty::{self, OpaqueHiddenType, OpaqueTypeKey, Region, RegionVid};
1212
use rustc_span::symbol::sym;
1313
use std::env;
1414
use std::fmt::Debug;
@@ -43,7 +43,7 @@ pub type PoloniusOutput = Output<RustcFacts>;
4343
/// closure requirements to propagate, and any generated errors.
4444
crate struct NllOutput<'tcx> {
4545
pub regioncx: RegionInferenceContext<'tcx>,
46-
pub opaque_type_values: VecMap<OpaqueTypeKey<'tcx>, Ty<'tcx>>,
46+
pub opaque_type_values: VecMap<OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>>,
4747
pub polonius_input: Option<Box<AllFacts>>,
4848
pub polonius_output: Option<Rc<PoloniusOutput>>,
4949
pub opt_closure_req: Option<ClosureRegionRequirements<'tcx>>,
@@ -305,7 +305,7 @@ pub(crate) fn compute_regions<'cx, 'tcx>(
305305
infcx.set_tainted_by_errors();
306306
}
307307

308-
let remapped_opaque_tys = regioncx.infer_opaque_types(&infcx, opaque_type_values, body.span);
308+
let remapped_opaque_tys = regioncx.infer_opaque_types(&infcx, opaque_type_values);
309309

310310
NllOutput {
311311
regioncx,
@@ -372,7 +372,7 @@ pub(super) fn dump_annotation<'a, 'tcx>(
372372
body: &Body<'tcx>,
373373
regioncx: &RegionInferenceContext<'tcx>,
374374
closure_region_requirements: &Option<ClosureRegionRequirements<'_>>,
375-
opaque_type_values: &VecMap<OpaqueTypeKey<'tcx>, Ty<'tcx>>,
375+
opaque_type_values: &VecMap<OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>>,
376376
errors: &mut crate::error::BorrowckErrors<'tcx>,
377377
) {
378378
let tcx = infcx.tcx;

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_data_structures::vec_map::VecMap;
33
use rustc_hir::OpaqueTyOrigin;
44
use rustc_infer::infer::InferCtxt;
55
use rustc_middle::ty::subst::GenericArgKind;
6-
use rustc_middle::ty::{self, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable};
6+
use rustc_middle::ty::{self, OpaqueHiddenType, OpaqueTypeKey, TyCtxt, TypeFoldable};
77
use rustc_span::Span;
88
use rustc_trait_selection::opaque_types::InferCtxtExt;
99

@@ -53,15 +53,12 @@ impl<'tcx> RegionInferenceContext<'tcx> {
5353
pub(crate) fn infer_opaque_types(
5454
&self,
5555
infcx: &InferCtxt<'_, 'tcx>,
56-
opaque_ty_decls: VecMap<OpaqueTypeKey<'tcx>, (Ty<'tcx>, Span, OpaqueTyOrigin)>,
57-
span: Span,
58-
) -> VecMap<OpaqueTypeKey<'tcx>, Ty<'tcx>> {
56+
opaque_ty_decls: VecMap<OpaqueTypeKey<'tcx>, (OpaqueHiddenType<'tcx>, OpaqueTyOrigin)>,
57+
) -> VecMap<OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>> {
5958
opaque_ty_decls
6059
.into_iter()
61-
.map(|(opaque_type_key, (concrete_type, decl_span, origin))| {
60+
.map(|(opaque_type_key, (concrete_type, origin))| {
6261
let substs = opaque_type_key.substs;
63-
// FIXME: why are the spans in decl_span often DUMMY_SP?
64-
let span = decl_span.substitute_dummy(span);
6562
debug!(?concrete_type, ?substs);
6663

6764
let mut subst_regions = vec![self.universal_regions.fr_static];
@@ -85,7 +82,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
8582
None => {
8683
subst_regions.push(vid);
8784
infcx.tcx.sess.delay_span_bug(
88-
span,
85+
concrete_type.span,
8986
"opaque type with non-universal region substs",
9087
);
9188
infcx.tcx.lifetimes.re_static
@@ -113,17 +110,18 @@ impl<'tcx> RegionInferenceContext<'tcx> {
113110
let remapped_type = infcx.infer_opaque_definition_from_instantiation(
114111
opaque_type_key,
115112
universal_concrete_type,
116-
span,
117113
);
118-
119-
(
114+
let ty = if check_opaque_type_parameter_valid(
115+
infcx.tcx,
120116
opaque_type_key,
121-
if check_opaque_type_parameter_valid(infcx.tcx, opaque_type_key, origin, span) {
122-
remapped_type
123-
} else {
124-
infcx.tcx.ty_error()
125-
},
126-
)
117+
origin,
118+
concrete_type.span,
119+
) {
120+
remapped_type
121+
} else {
122+
infcx.tcx.ty_error()
123+
};
124+
(opaque_type_key, OpaqueHiddenType { ty, span: concrete_type.span })
127125
})
128126
.collect()
129127
}

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ use rustc_middle::ty::cast::CastTy;
3131
use rustc_middle::ty::fold::TypeFoldable;
3232
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef, UserSubsts};
3333
use rustc_middle::ty::{
34-
self, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, OpaqueTypeKey, RegionVid,
35-
ToPredicate, Ty, TyCtxt, UserType, UserTypeAnnotationIndex,
34+
self, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, OpaqueHiddenType,
35+
OpaqueTypeKey, RegionVid, ToPredicate, Ty, TyCtxt, UserType, UserTypeAnnotationIndex,
3636
};
3737
use rustc_span::def_id::CRATE_DEF_ID;
3838
use rustc_span::{Span, DUMMY_SP};
@@ -225,21 +225,21 @@ pub(crate) fn type_check<'mir, 'tcx>(
225225
),
226226
)
227227
.unwrap();
228-
let mut hidden_type = infcx.resolve_vars_if_possible(decl.hidden_type.ty);
228+
let mut hidden_type = infcx.resolve_vars_if_possible(decl.hidden_type);
229229
trace!(
230230
"finalized opaque type {:?} to {:#?}",
231231
opaque_type_key,
232-
hidden_type.kind()
232+
hidden_type.ty.kind()
233233
);
234234
if hidden_type.has_infer_types_or_consts() {
235235
infcx.tcx.sess.delay_span_bug(
236236
decl.hidden_type.span,
237-
&format!("could not resolve {:#?}", hidden_type.kind()),
237+
&format!("could not resolve {:#?}", hidden_type.ty.kind()),
238238
);
239-
hidden_type = infcx.tcx.ty_error();
239+
hidden_type.ty = infcx.tcx.ty_error();
240240
}
241241

242-
(opaque_type_key, (hidden_type, decl.hidden_type.span, decl.origin))
242+
(opaque_type_key, (hidden_type, decl.origin))
243243
})
244244
.collect()
245245
},
@@ -905,7 +905,7 @@ struct BorrowCheckContext<'a, 'tcx> {
905905
crate struct MirTypeckResults<'tcx> {
906906
crate constraints: MirTypeckRegionConstraints<'tcx>,
907907
crate universal_region_relations: Frozen<UniversalRegionRelations<'tcx>>,
908-
crate opaque_type_values: VecMap<OpaqueTypeKey<'tcx>, (Ty<'tcx>, Span, OpaqueTyOrigin)>,
908+
crate opaque_type_values: VecMap<OpaqueTypeKey<'tcx>, (OpaqueHiddenType<'tcx>, OpaqueTyOrigin)>,
909909
}
910910

911911
/// A collection of region constraints that must be satisfied for the

compiler/rustc_infer/src/infer/opaque_types.rs

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use rustc_hir as hir;
88
use rustc_middle::traits::ObligationCause;
99
use rustc_middle::ty::fold::BottomUpFolder;
1010
use rustc_middle::ty::subst::{GenericArgKind, Subst};
11-
use rustc_middle::ty::{self, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable, TypeVisitor};
11+
use rustc_middle::ty::{
12+
self, OpaqueHiddenType, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable, TypeVisitor,
13+
};
1214
use rustc_span::Span;
1315

1416
use std::ops::ControlFlow;
@@ -35,38 +37,6 @@ pub struct OpaqueTypeDecl<'tcx> {
3537
pub origin: hir::OpaqueTyOrigin,
3638
}
3739

38-
#[derive(Copy, Clone, Debug, TypeFoldable)]
39-
pub struct OpaqueHiddenType<'tcx> {
40-
/// The span of this particular definition of the opaque type. So
41-
/// for example:
42-
///
43-
/// ```ignore (incomplete snippet)
44-
/// type Foo = impl Baz;
45-
/// fn bar() -> Foo {
46-
/// // ^^^ This is the span we are looking for!
47-
/// }
48-
/// ```
49-
///
50-
/// In cases where the fn returns `(impl Trait, impl Trait)` or
51-
/// other such combinations, the result is currently
52-
/// over-approximated, but better than nothing.
53-
pub span: Span,
54-
55-
/// The type variable that represents the value of the opaque type
56-
/// that we require. In other words, after we compile this function,
57-
/// we will be created a constraint like:
58-
///
59-
/// Foo<'a, T> = ?C
60-
///
61-
/// where `?C` is the value of this type variable. =) It may
62-
/// naturally refer to the type and lifetime parameters in scope
63-
/// in this function, though ultimately it should only reference
64-
/// those that are arguments to `Foo` in the constraint above. (In
65-
/// other words, `?C` should not include `'b`, even though it's a
66-
/// lifetime parameter on `foo`.)
67-
pub ty: Ty<'tcx>,
68-
}
69-
7040
impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
7141
pub fn handle_opaque_type(
7242
&self,

compiler/rustc_infer/src/infer/opaque_types/table.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use rustc_data_structures::undo_log::UndoLogs;
22
use rustc_hir::OpaqueTyOrigin;
3-
use rustc_middle::ty::{self, OpaqueTypeKey, Ty};
3+
use rustc_middle::ty::{self, OpaqueHiddenType, OpaqueTypeKey, Ty};
44
use rustc_span::DUMMY_SP;
55

66
use crate::infer::{InferCtxtUndoLogs, UndoLog};
77

8-
use super::{OpaqueHiddenType, OpaqueTypeDecl, OpaqueTypeMap};
8+
use super::{OpaqueTypeDecl, OpaqueTypeMap};
99

1010
#[derive(Default, Debug, Clone)]
1111
pub struct OpaqueTypeStorage<'tcx> {

compiler/rustc_infer/src/infer/undo_log.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ use rustc_data_structures::snapshot_vec as sv;
44
use rustc_data_structures::undo_log::{Rollback, UndoLogs};
55
use rustc_data_structures::unify as ut;
66
use rustc_middle::infer::unify_key::RegionVidKey;
7-
use rustc_middle::ty::{self, OpaqueTypeKey};
7+
use rustc_middle::ty::{self, OpaqueHiddenType, OpaqueTypeKey};
88

99
use crate::{
1010
infer::{region_constraints, type_variable, InferCtxtInner},
1111
traits,
1212
};
1313

14-
use super::opaque_types::OpaqueHiddenType;
15-
1614
pub struct Snapshot<'tcx> {
1715
pub(crate) undo_len: usize,
1816
_marker: PhantomData<&'tcx ()>,

compiler/rustc_middle/src/mir/query.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Values computed by queries that use MIR.
22
33
use crate::mir::{Body, Promoted};
4-
use crate::ty::{self, Ty, TyCtxt};
4+
use crate::ty::{self, OpaqueHiddenType, Ty, TyCtxt};
55
use rustc_data_structures::stable_map::FxHashMap;
66
use rustc_data_structures::vec_map::VecMap;
77
use rustc_errors::ErrorGuaranteed;
@@ -242,7 +242,7 @@ pub struct BorrowCheckResult<'tcx> {
242242
/// All the opaque types that are restricted to concrete types
243243
/// by this function. Unlike the value in `TypeckResults`, this has
244244
/// unerased regions.
245-
pub concrete_opaque_types: VecMap<OpaqueTypeKey<'tcx>, Ty<'tcx>>,
245+
pub concrete_opaque_types: VecMap<OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>>,
246246
pub closure_requirements: Option<ClosureRegionRequirements<'tcx>>,
247247
pub used_mut_upvars: SmallVec<[Field; 8]>,
248248
pub tainted_by_errors: Option<ErrorGuaranteed>,

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,38 @@ pub struct OpaqueTypeKey<'tcx> {
10811081
pub substs: SubstsRef<'tcx>,
10821082
}
10831083

1084+
#[derive(Copy, Clone, Debug, TypeFoldable, HashStable, TyEncodable, TyDecodable)]
1085+
pub struct OpaqueHiddenType<'tcx> {
1086+
/// The span of this particular definition of the opaque type. So
1087+
/// for example:
1088+
///
1089+
/// ```ignore (incomplete snippet)
1090+
/// type Foo = impl Baz;
1091+
/// fn bar() -> Foo {
1092+
/// // ^^^ This is the span we are looking for!
1093+
/// }
1094+
/// ```
1095+
///
1096+
/// In cases where the fn returns `(impl Trait, impl Trait)` or
1097+
/// other such combinations, the result is currently
1098+
/// over-approximated, but better than nothing.
1099+
pub span: Span,
1100+
1101+
/// The type variable that represents the value of the opaque type
1102+
/// that we require. In other words, after we compile this function,
1103+
/// we will be created a constraint like:
1104+
///
1105+
/// Foo<'a, T> = ?C
1106+
///
1107+
/// where `?C` is the value of this type variable. =) It may
1108+
/// naturally refer to the type and lifetime parameters in scope
1109+
/// in this function, though ultimately it should only reference
1110+
/// those that are arguments to `Foo` in the constraint above. (In
1111+
/// other words, `?C` should not include `'b`, even though it's a
1112+
/// lifetime parameter on `foo`.)
1113+
pub ty: Ty<'tcx>,
1114+
}
1115+
10841116
rustc_index::newtype_index! {
10851117
/// "Universes" are used during type- and trait-checking in the
10861118
/// presence of `for<..>` binders to control what sets of names are

compiler/rustc_trait_selection/src/opaque_types.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ use rustc_infer::infer::error_reporting::unexpected_hidden_region_diagnostic;
55
use rustc_infer::infer::InferCtxt;
66
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
77
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, InternalSubsts};
8-
use rustc_middle::ty::{self, OpaqueTypeKey, Ty, TyCtxt};
8+
use rustc_middle::ty::{self, OpaqueHiddenType, OpaqueTypeKey, Ty, TyCtxt};
99
use rustc_span::Span;
1010

1111
pub trait InferCtxtExt<'tcx> {
1212
fn infer_opaque_definition_from_instantiation(
1313
&self,
1414
opaque_type_key: OpaqueTypeKey<'tcx>,
15-
instantiated_ty: Ty<'tcx>,
16-
span: Span,
15+
instantiated_ty: OpaqueHiddenType<'tcx>,
1716
) -> Ty<'tcx>;
1817
}
1918

@@ -45,8 +44,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
4544
fn infer_opaque_definition_from_instantiation(
4645
&self,
4746
opaque_type_key: OpaqueTypeKey<'tcx>,
48-
instantiated_ty: Ty<'tcx>,
49-
span: Span,
47+
instantiated_ty: OpaqueHiddenType<'tcx>,
5048
) -> Ty<'tcx> {
5149
if self.is_tainted_by_errors() {
5250
return self.tcx.ty_error();
@@ -69,12 +67,12 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
6967
// Convert the type from the function into a type valid outside
7068
// the function, by replacing invalid regions with 'static,
7169
// after producing an error for each of them.
72-
let definition_ty = instantiated_ty.fold_with(&mut ReverseMapper::new(
70+
let definition_ty = instantiated_ty.ty.fold_with(&mut ReverseMapper::new(
7371
self.tcx,
7472
def_id,
7573
map,
76-
instantiated_ty,
77-
span,
74+
instantiated_ty.ty,
75+
instantiated_ty.span,
7876
));
7977
debug!(?definition_ty);
8078

0 commit comments

Comments
 (0)