Skip to content

Commit 70d39ab

Browse files
committed
Remap hidden types from typeck before storing them in the TypeckResult
1 parent 9eb69e8 commit 70d39ab

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
226226
}
227227

228228
let definition_ty = instantiated_ty
229-
.remap_generic_params_to_declaration_params(opaque_type_key, self.tcx)
229+
.remap_generic_params_to_declaration_params(opaque_type_key, self.tcx, false)
230230
.ty;
231231

232232
if !check_opaque_type_parameter_valid(

compiler/rustc_hir_analysis/src/check/writeback.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,8 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
536536
let opaque_types =
537537
self.fcx.infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
538538
for (opaque_type_key, decl) in opaque_types {
539-
let hidden_type = self.resolve(decl.hidden_type.ty, &decl.hidden_type.span);
539+
let hidden_type = self.resolve(decl.hidden_type, &decl.hidden_type.span);
540+
let opaque_type_key = self.resolve(opaque_type_key, &decl.hidden_type.span);
540541

541542
struct RecursionChecker {
542543
def_id: LocalDefId,
@@ -559,6 +560,14 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
559560
continue;
560561
}
561562

563+
let hidden_type = hidden_type
564+
.remap_generic_params_to_declaration_params(
565+
opaque_type_key,
566+
self.fcx.infcx.tcx,
567+
true,
568+
)
569+
.ty;
570+
562571
self.typeck_results.concrete_opaque_types.insert(opaque_type_key.def_id, hidden_type);
563572
}
564573
}

compiler/rustc_middle/src/ty/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,8 @@ impl<'tcx> OpaqueHiddenType<'tcx> {
13071307
self,
13081308
opaque_type_key: OpaqueTypeKey<'tcx>,
13091309
tcx: TyCtxt<'tcx>,
1310+
// typeck errors have subpar spans for opaque types, so delay error reporting until borrowck.
1311+
ignore_errors: bool,
13101312
) -> Self {
13111313
let OpaqueTypeKey { def_id, substs } = opaque_type_key;
13121314

@@ -1325,7 +1327,7 @@ impl<'tcx> OpaqueHiddenType<'tcx> {
13251327
// Convert the type from the function into a type valid outside
13261328
// the function, by replacing invalid regions with 'static,
13271329
// after producing an error for each of them.
1328-
self.fold_with(&mut opaque_types::ReverseMapper::new(tcx, map, self.span))
1330+
self.fold_with(&mut opaque_types::ReverseMapper::new(tcx, map, self.span, ignore_errors))
13291331
}
13301332
}
13311333

compiler/rustc_middle/src/ty/opaque_types.rs

+28-15
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,16 @@ use rustc_span::Span;
1010
pub(super) struct ReverseMapper<'tcx> {
1111
tcx: TyCtxt<'tcx>,
1212
map: FxHashMap<GenericArg<'tcx>, GenericArg<'tcx>>,
13+
/// see call sites to fold_kind_no_missing_regions_error
14+
/// for an explanation of this field.
1315
do_not_error: bool,
1416

17+
/// We do not want to emit any errors in typeck because
18+
/// the spans in typeck are subpar at the moment.
19+
/// Borrowck will do the same work again (this time with
20+
/// lifetime information) and thus report better errors.
21+
ignore_errors: bool,
22+
1523
/// Span of function being checked.
1624
span: Span,
1725
}
@@ -21,8 +29,9 @@ impl<'tcx> ReverseMapper<'tcx> {
2129
tcx: TyCtxt<'tcx>,
2230
map: FxHashMap<GenericArg<'tcx>, GenericArg<'tcx>>,
2331
span: Span,
32+
ignore_errors: bool,
2433
) -> Self {
25-
Self { tcx, map, do_not_error: false, span }
34+
Self { tcx, map, do_not_error: false, ignore_errors, span }
2635
}
2736

2837
fn fold_kind_no_missing_regions_error(&mut self, kind: GenericArg<'tcx>) -> GenericArg<'tcx> {
@@ -156,17 +165,19 @@ impl<'tcx> TypeFolder<'tcx> for ReverseMapper<'tcx> {
156165
Some(u) => panic!("type mapped to unexpected kind: {:?}", u),
157166
None => {
158167
debug!(?param, ?self.map);
159-
self.tcx
160-
.sess
161-
.struct_span_err(
162-
self.span,
163-
&format!(
164-
"type parameter `{}` is part of concrete type but not \
168+
if !self.ignore_errors {
169+
self.tcx
170+
.sess
171+
.struct_span_err(
172+
self.span,
173+
&format!(
174+
"type parameter `{}` is part of concrete type but not \
165175
used in parameter list for the `impl Trait` type alias",
166-
ty
167-
),
168-
)
169-
.emit();
176+
ty
177+
),
178+
)
179+
.emit();
180+
}
170181

171182
self.tcx().ty_error()
172183
}
@@ -189,10 +200,12 @@ impl<'tcx> TypeFolder<'tcx> for ReverseMapper<'tcx> {
189200
Some(GenericArgKind::Const(c1)) => c1,
190201
Some(u) => panic!("const mapped to unexpected kind: {:?}", u),
191202
None => {
192-
self.tcx.sess.emit_err(ty::ConstNotUsedTraitAlias {
193-
ct: ct.to_string(),
194-
span: self.span,
195-
});
203+
if !self.ignore_errors {
204+
self.tcx.sess.emit_err(ty::ConstNotUsedTraitAlias {
205+
ct: ct.to_string(),
206+
span: self.span,
207+
});
208+
}
196209

197210
self.tcx().const_error(ct.ty())
198211
}

0 commit comments

Comments
 (0)