Skip to content

Commit f23bca7

Browse files
committed
Address review comments
1 parent 93ac5bc commit f23bca7

File tree

6 files changed

+67
-55
lines changed

6 files changed

+67
-55
lines changed

src/librustc/infer/opaque_types/mod.rs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -500,9 +500,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
500500
hir::OpaqueTyOrigin::AsyncFn => return false,
501501

502502
// Otherwise, generate the label we'll use in the error message.
503-
hir::OpaqueTyOrigin::TypeAlias => "impl Trait",
504-
hir::OpaqueTyOrigin::FnReturn => "impl Trait",
505-
hir::OpaqueTyOrigin::Misc => "impl Trait",
503+
hir::OpaqueTyOrigin::TypeAlias
504+
| hir::OpaqueTyOrigin::FnReturn
505+
| hir::OpaqueTyOrigin::Misc => "impl Trait",
506506
};
507507
let msg = format!("ambiguous lifetime bound in `{}`", context_name);
508508
let mut err = self.tcx.sess.struct_span_err(span, &msg);
@@ -814,26 +814,37 @@ impl TypeFolder<'tcx> for ReverseMapper<'tcx> {
814814

815815
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
816816
match r {
817-
// Ignore bound regions that appear in the type, they don't need to
818-
// be remapped (e.g., this would ignore `'r` in a type like
819-
// `for<'r> fn(&'r u32)`.
820-
ty::ReLateBound(..)
821-
822-
// If regions have been erased, don't try to unerase them.
823-
| ty::ReErased
824-
825-
// ignore `'static`, as that can appear anywhere
826-
| ty::ReStatic => return r,
827-
828-
_ => {}
817+
// Ignore bound regions and `'static` regions that appear in the
818+
// type, we only need to remap regions that reference lifetimes
819+
// from the function declaraion.
820+
// This would ignore `'r` in a type like `for<'r> fn(&'r u32)`.
821+
ty::ReLateBound(..) | ty::ReStatic => return r,
822+
823+
// If regions have been erased (by writeback), don't try to unerase
824+
// them.
825+
ty::ReErased => return r,
826+
827+
// The regions that we expect from borrow checking.
828+
ty::ReEarlyBound(_) | ty::ReFree(_) | ty::ReEmpty(ty::UniverseIndex::ROOT) => {}
829+
830+
ty::ReEmpty(_)
831+
| ty::RePlaceholder(_)
832+
| ty::ReVar(_)
833+
| ty::ReScope(_)
834+
| ty::ReClosureBound(_) => {
835+
// All of the regions in the type should either have been
836+
// erased by writeback, or mapped back to named regions by
837+
// borrow checking.
838+
bug!("unexpected region kind in opaque type: {:?}", r);
839+
}
829840
}
830841

831842
let generics = self.tcx().generics_of(self.opaque_type_def_id);
832843
match self.map.get(&r.into()).map(|k| k.unpack()) {
833844
Some(GenericArgKind::Lifetime(r1)) => r1,
834845
Some(u) => panic!("region mapped to unexpected kind: {:?}", u),
835846
None if self.map_missing_regions_to_empty || self.tainted_by_errors => {
836-
self.tcx.lifetimes.re_empty
847+
self.tcx.lifetimes.re_root_empty
837848
}
838849
None if generics.parent.is_some() => {
839850
if let Some(hidden_ty) = self.hidden_ty.take() {
@@ -862,7 +873,7 @@ impl TypeFolder<'tcx> for ReverseMapper<'tcx> {
862873
)
863874
.emit();
864875

865-
self.tcx().mk_region(ty::ReStatic)
876+
self.tcx().lifetimes.re_static
866877
}
867878
}
868879
}

src/librustc_ast_lowering/lib.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,17 +1723,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17231723
)
17241724
} else {
17251725
match decl.output {
1726-
FunctionRetTy::Ty(ref ty) => match in_band_ty_params {
1727-
Some((def_id, _)) if impl_trait_return_allow => {
1728-
hir::FunctionRetTy::Return(self.lower_ty(
1729-
ty,
1730-
ImplTraitContext::OpaqueTy(Some(def_id), hir::OpaqueTyOrigin::FnReturn),
1731-
))
1732-
}
1733-
_ => hir::FunctionRetTy::Return(
1734-
self.lower_ty(ty, ImplTraitContext::disallowed()),
1735-
),
1736-
},
1726+
FunctionRetTy::Ty(ref ty) => {
1727+
let context = match in_band_ty_params {
1728+
Some((def_id, _)) if impl_trait_return_allow => {
1729+
ImplTraitContext::OpaqueTy(Some(def_id), hir::OpaqueTyOrigin::FnReturn)
1730+
}
1731+
_ => ImplTraitContext::disallowed(),
1732+
};
1733+
hir::FunctionRetTy::Return(self.lower_ty(ty, context))
1734+
}
17371735
FunctionRetTy::Default(span) => hir::FunctionRetTy::DefaultReturn(span),
17381736
}
17391737
};
@@ -1961,10 +1959,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19611959
) -> hir::GenericBound<'hir> {
19621960
// Compute the `T` in `Future<Output = T>` from the return type.
19631961
let output_ty = match output {
1964-
FunctionRetTy::Ty(ty) => self.lower_ty(
1965-
ty,
1966-
ImplTraitContext::OpaqueTy(Some(fn_def_id), hir::OpaqueTyOrigin::FnReturn),
1967-
),
1962+
FunctionRetTy::Ty(ty) => {
1963+
// Not `OpaqueTyOrigin::AsyncFn`: that's only used for the
1964+
// `impl Future` opaque type that `async fn` implicitly
1965+
// generates.
1966+
let context =
1967+
ImplTraitContext::OpaqueTy(Some(fn_def_id), hir::OpaqueTyOrigin::FnReturn);
1968+
self.lower_ty(ty, context)
1969+
}
19681970
FunctionRetTy::Default(ret_ty_span) => self.arena.alloc(self.ty_tup(*ret_ty_span, &[])),
19691971
};
19701972

src/librustc_mir/borrow_check/type_check/input_output.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
120120
self.mir_def_id,
121121
Locations::All(output_span),
122122
ConstraintCategory::BoringNoLocation,
123-
true,
124123
) {
125124
span_mirbug!(
126125
self,
@@ -144,7 +143,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
144143
self.mir_def_id,
145144
Locations::All(output_span),
146145
ConstraintCategory::BoringNoLocation,
147-
false,
148146
) {
149147
span_mirbug!(
150148
self,

src/librustc_mir/borrow_check/type_check/mod.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,14 +1122,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
11221122
// the resulting inferend values are stored with the
11231123
// def-id of the base function.
11241124
let parent_def_id = self.tcx().closure_base_def_id(self.mir_def_id);
1125-
return self.eq_opaque_type_and_type(
1126-
sub,
1127-
sup,
1128-
parent_def_id,
1129-
locations,
1130-
category,
1131-
false,
1132-
);
1125+
return self.eq_opaque_type_and_type(sub, sup, parent_def_id, locations, category);
11331126
} else {
11341127
return Err(terr);
11351128
}
@@ -1195,7 +1188,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
11951188
anon_owner_def_id: DefId,
11961189
locations: Locations,
11971190
category: ConstraintCategory,
1198-
is_function_return: bool,
11991191
) -> Fallible<()> {
12001192
debug!(
12011193
"eq_opaque_type_and_type( \
@@ -1273,13 +1265,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
12731265
opaque_decl.concrete_ty, resolved_ty, renumbered_opaque_defn_ty,
12741266
);
12751267

1276-
if !concrete_is_opaque
1277-
|| (is_function_return
1278-
&& matches!(opaque_decl.origin, hir::OpaqueTyOrigin::FnReturn))
1279-
{
1280-
// For return position impl Trait, the function
1281-
// return is the only possible definition site, so
1282-
// always record it.
1268+
if !concrete_is_opaque {
12831269
obligations.add(
12841270
infcx
12851271
.at(&ObligationCause::dummy(), param_env)

src/librustc_typeck/collect.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,7 +1471,9 @@ fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
14711471
&tcx.mir_borrowck(owner).concrete_opaque_types
14721472
}
14731473
hir::OpaqueTyOrigin::Misc => {
1474-
// We shouldn't leak borrowck results through impl Trait in bindings.
1474+
// We shouldn't leak borrowck results through impl trait in bindings.
1475+
// For example, we shouldn't be able to tell if `x` in
1476+
// `let x: impl Sized + 'a = &()` has type `&'static ()` or `&'a ()`.
14751477
&tcx.typeck_tables_of(owner).concrete_opaque_types
14761478
}
14771479
hir::OpaqueTyOrigin::TypeAlias => {
@@ -1482,17 +1484,27 @@ fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
14821484
.get(&def_id)
14831485
.map(|opaque| opaque.concrete_type)
14841486
.unwrap_or_else(|| {
1485-
// This can occur if some error in the
1486-
// owner fn prevented us from populating
1487-
// the `concrete_opaque_types` table.
14881487
tcx.sess.delay_span_bug(
14891488
DUMMY_SP,
14901489
&format!(
14911490
"owner {:?} has no opaque type for {:?} in its tables",
14921491
owner, def_id,
14931492
),
14941493
);
1495-
tcx.types.err
1494+
if tcx.typeck_tables_of(owner).tainted_by_errors {
1495+
// Some error in the
1496+
// owner fn prevented us from populating
1497+
// the `concrete_opaque_types` table.
1498+
tcx.types.err
1499+
} else {
1500+
// We failed to resolve the opaque type or it
1501+
// resolves to itself. Return the non-revealed
1502+
// type, which should result in E0720.
1503+
tcx.mk_opaque(
1504+
def_id,
1505+
InternalSubsts::identity_for_item(tcx, def_id),
1506+
)
1507+
}
14961508
});
14971509
debug!("concrete_ty = {:?}", concrete_ty);
14981510
if concrete_ty.has_erased_regions() {

src/librustc_typeck/impl_wf_check.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ fn enforce_impl_params_are_constrained(
131131
}
132132
}
133133
ty::AssocKind::OpaqueTy => {
134+
// We don't know which lifetimes appear in the actual
135+
// opaque type, so use all of the lifetimes that appear
136+
// in the type's predicates.
134137
let predicates = tcx.predicates_of(def_id).instantiate_identity(tcx);
135138
cgp::parameters_for(&predicates, true)
136139
}

0 commit comments

Comments
 (0)