Skip to content

Commit 91fa570

Browse files
committed
Have the spans of TAIT type conflict errors point to the actual site instead of the owning function
1 parent 0c292c9 commit 91fa570

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
@@ -9,7 +9,7 @@ use rustc_middle::mir::{
99
BasicBlock, Body, ClosureOutlivesSubject, ClosureRegionRequirements, LocalKind, Location,
1010
Promoted,
1111
};
12-
use rustc_middle::ty::{self, OpaqueTypeKey, RegionKind, RegionVid, Ty};
12+
use rustc_middle::ty::{self, OpaqueHiddenType, OpaqueTypeKey, RegionKind, RegionVid};
1313
use rustc_span::symbol::sym;
1414
use std::env;
1515
use std::fmt::Debug;
@@ -44,7 +44,7 @@ pub type PoloniusOutput = Output<RustcFacts>;
4444
/// closure requirements to propagate, and any generated errors.
4545
crate struct NllOutput<'tcx> {
4646
pub regioncx: RegionInferenceContext<'tcx>,
47-
pub opaque_type_values: VecMap<OpaqueTypeKey<'tcx>, Ty<'tcx>>,
47+
pub opaque_type_values: VecMap<OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>>,
4848
pub polonius_input: Option<Box<AllFacts>>,
4949
pub polonius_output: Option<Rc<PoloniusOutput>>,
5050
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_buffer: &mut Vec<Diagnostic>,
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
@@ -30,8 +30,8 @@ use rustc_middle::ty::cast::CastTy;
3030
use rustc_middle::ty::fold::TypeFoldable;
3131
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef, UserSubsts};
3232
use rustc_middle::ty::{
33-
self, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, OpaqueTypeKey, RegionVid,
34-
ToPredicate, Ty, TyCtxt, UserType, UserTypeAnnotationIndex,
33+
self, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, OpaqueHiddenType,
34+
OpaqueTypeKey, RegionVid, ToPredicate, Ty, TyCtxt, UserType, UserTypeAnnotationIndex,
3535
};
3636
use rustc_span::def_id::CRATE_DEF_ID;
3737
use rustc_span::{Span, DUMMY_SP};
@@ -213,21 +213,21 @@ pub(crate) fn type_check<'mir, 'tcx>(
213213
),
214214
)
215215
.unwrap();
216-
let mut hidden_type = infcx.resolve_vars_if_possible(decl.hidden_type.ty);
216+
let mut hidden_type = infcx.resolve_vars_if_possible(decl.hidden_type);
217217
trace!(
218218
"finalized opaque type {:?} to {:#?}",
219219
opaque_type_key,
220-
hidden_type.kind()
220+
hidden_type.ty.kind()
221221
);
222222
if hidden_type.has_infer_types_or_consts() {
223223
infcx.tcx.sess.delay_span_bug(
224224
decl.hidden_type.span,
225-
&format!("could not resolve {:#?}", hidden_type.kind()),
225+
&format!("could not resolve {:#?}", hidden_type.ty.kind()),
226226
);
227-
hidden_type = infcx.tcx.ty_error();
227+
hidden_type.ty = infcx.tcx.ty_error();
228228
}
229229

230-
(opaque_type_key, (hidden_type, decl.hidden_type.span, decl.origin))
230+
(opaque_type_key, (hidden_type, decl.origin))
231231
})
232232
.collect()
233233
},
@@ -893,7 +893,7 @@ struct BorrowCheckContext<'a, 'tcx> {
893893
crate struct MirTypeckResults<'tcx> {
894894
crate constraints: MirTypeckRegionConstraints<'tcx>,
895895
crate universal_region_relations: Frozen<UniversalRegionRelations<'tcx>>,
896-
crate opaque_type_values: VecMap<OpaqueTypeKey<'tcx>, (Ty<'tcx>, Span, OpaqueTyOrigin)>,
896+
crate opaque_type_values: VecMap<OpaqueTypeKey<'tcx>, (OpaqueHiddenType<'tcx>, OpaqueTyOrigin)>,
897897
}
898898

899899
/// 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)]
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::sync::Lrc;
66
use rustc_data_structures::vec_map::VecMap;
77
use rustc_errors::ErrorReported;
@@ -211,7 +211,7 @@ pub struct BorrowCheckResult<'tcx> {
211211
/// All the opaque types that are restricted to concrete types
212212
/// by this function. Unlike the value in `TypeckResults`, this has
213213
/// unerased regions.
214-
pub concrete_opaque_types: VecMap<OpaqueTypeKey<'tcx>, Ty<'tcx>>,
214+
pub concrete_opaque_types: VecMap<OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>>,
215215
pub closure_requirements: Option<ClosureRegionRequirements<'tcx>>,
216216
pub used_mut_upvars: SmallVec<[Field; 8]>,
217217
}

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,38 @@ pub struct OpaqueTypeKey<'tcx> {
10681068
pub substs: SubstsRef<'tcx>,
10691069
}
10701070

1071+
#[derive(Copy, Clone, Debug, TypeFoldable, HashStable, TyEncodable, TyDecodable)]
1072+
pub struct OpaqueHiddenType<'tcx> {
1073+
/// The span of this particular definition of the opaque type. So
1074+
/// for example:
1075+
///
1076+
/// ```ignore (incomplete snippet)
1077+
/// type Foo = impl Baz;
1078+
/// fn bar() -> Foo {
1079+
/// // ^^^ This is the span we are looking for!
1080+
/// }
1081+
/// ```
1082+
///
1083+
/// In cases where the fn returns `(impl Trait, impl Trait)` or
1084+
/// other such combinations, the result is currently
1085+
/// over-approximated, but better than nothing.
1086+
pub span: Span,
1087+
1088+
/// The type variable that represents the value of the opaque type
1089+
/// that we require. In other words, after we compile this function,
1090+
/// we will be created a constraint like:
1091+
///
1092+
/// Foo<'a, T> = ?C
1093+
///
1094+
/// where `?C` is the value of this type variable. =) It may
1095+
/// naturally refer to the type and lifetime parameters in scope
1096+
/// in this function, though ultimately it should only reference
1097+
/// those that are arguments to `Foo` in the constraint above. (In
1098+
/// other words, `?C` should not include `'b`, even though it's a
1099+
/// lifetime parameter on `foo`.)
1100+
pub ty: Ty<'tcx>,
1101+
}
1102+
10711103
rustc_index::newtype_index! {
10721104
/// "Universes" are used during type- and trait-checking in the
10731105
/// 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)