Skip to content

Commit 935dc07

Browse files
committedDec 19, 2022
Auto merge of #103600 - compiler-errors:early-binder-nits, r=spastorino
Address some `EarlyBinder` nits
2 parents 7ab8038 + f705d64 commit 935dc07

File tree

5 files changed

+27
-45
lines changed

5 files changed

+27
-45
lines changed
 

‎compiler/rustc_hir_analysis/src/astconv/mod.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@ use rustc_hir::intravisit::{walk_generics, Visitor as _};
2626
use rustc_hir::{GenericArg, GenericArgs, OpaqueTyOrigin};
2727
use rustc_middle::middle::stability::AllowUnstable;
2828
use rustc_middle::ty::subst::{self, GenericArgKind, InternalSubsts, SubstsRef};
29-
use rustc_middle::ty::DynKind;
3029
use rustc_middle::ty::GenericParamDefKind;
31-
use rustc_middle::ty::{
32-
self, Const, DefIdTree, EarlyBinder, IsSuggestable, Ty, TyCtxt, TypeVisitable,
33-
};
30+
use rustc_middle::ty::{self, Const, DefIdTree, IsSuggestable, Ty, TyCtxt, TypeVisitable};
31+
use rustc_middle::ty::{DynKind, EarlyBinder};
3432
use rustc_session::lint::builtin::{AMBIGUOUS_ASSOCIATED_ITEMS, BARE_TRAIT_OBJECTS};
3533
use rustc_span::edition::Edition;
3634
use rustc_span::lev_distance::find_best_match_for_name;
@@ -490,7 +488,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
490488
self.astconv
491489
.normalize_ty(
492490
self.span,
493-
EarlyBinder(tcx.at(self.span).type_of(param.def_id))
491+
tcx.at(self.span)
492+
.bound_type_of(param.def_id)
494493
.subst(tcx, substs),
495494
)
496495
.into()
@@ -1255,10 +1254,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
12551254
item_segment: &hir::PathSegment<'_>,
12561255
) -> Ty<'tcx> {
12571256
let substs = self.ast_path_substs_for_ty(span, did, item_segment);
1258-
self.normalize_ty(
1259-
span,
1260-
EarlyBinder(self.tcx().at(span).type_of(did)).subst(self.tcx(), substs),
1261-
)
1257+
self.normalize_ty(span, self.tcx().at(span).bound_type_of(did).subst(self.tcx(), substs))
12621258
}
12631259

12641260
fn conv_object_ty_poly_trait_ref(

‎compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+7-27
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_middle::ty::error::TypeError;
2222
use rustc_middle::ty::fold::TypeFoldable;
2323
use rustc_middle::ty::visit::TypeVisitable;
2424
use rustc_middle::ty::{
25-
self, AdtKind, CanonicalUserType, DefIdTree, EarlyBinder, GenericParamDefKind, Ty, UserType,
25+
self, AdtKind, CanonicalUserType, DefIdTree, GenericParamDefKind, Ty, UserType,
2626
};
2727
use rustc_middle::ty::{GenericArgKind, InternalSubsts, SubstsRef, UserSelfTy, UserSubsts};
2828
use rustc_session::lint;
@@ -333,22 +333,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
333333
}
334334
}
335335

336-
/// Basically whenever we are converting from a type scheme into
337-
/// the fn body space, we always want to normalize associated
338-
/// types as well. This function combines the two.
339-
fn instantiate_type_scheme<T>(&self, span: Span, substs: SubstsRef<'tcx>, value: T) -> T
340-
where
341-
T: TypeFoldable<'tcx>,
342-
{
343-
debug!("instantiate_type_scheme(value={:?}, substs={:?})", value, substs);
344-
let value = EarlyBinder(value).subst(self.tcx, substs);
345-
let result = self.normalize(span, value);
346-
debug!("instantiate_type_scheme = {:?}", result);
347-
result
348-
}
349-
350-
/// As `instantiate_type_scheme`, but for the bounds found in a
351-
/// generic type scheme.
336+
/// Instantiates and normalizes the bounds for a given item
352337
pub(in super::super) fn instantiate_bounds(
353338
&self,
354339
span: Span,
@@ -1160,10 +1145,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11601145
};
11611146
let def_id = res.def_id();
11621147

1163-
// The things we are substituting into the type should not contain
1164-
// escaping late-bound regions, and nor should the base type scheme.
1165-
let ty = tcx.type_of(def_id);
1166-
11671148
let arg_count = GenericArgCountResult {
11681149
explicit_late_bound,
11691150
correct: if infer_args_for_err.is_empty() {
@@ -1286,8 +1267,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12861267
},
12871268
)
12881269
});
1289-
assert!(!substs.has_escaping_bound_vars());
1290-
assert!(!ty.has_escaping_bound_vars());
12911270

12921271
// First, store the "user substs" for later.
12931272
self.write_user_type_annotation_from_substs(hir_id, def_id, substs, user_self_ty);
@@ -1296,17 +1275,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12961275

12971276
// Substitute the values for the type parameters into the type of
12981277
// the referenced item.
1299-
let ty_substituted = self.instantiate_type_scheme(span, &substs, ty);
1278+
let ty = tcx.bound_type_of(def_id);
1279+
assert!(!substs.has_escaping_bound_vars());
1280+
assert!(!ty.0.has_escaping_bound_vars());
1281+
let ty_substituted = self.normalize(span, ty.subst(tcx, substs));
13001282

13011283
if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty {
13021284
// In the case of `Foo<T>::method` and `<Foo<T>>::method`, if `method`
13031285
// is inherent, there is no `Self` parameter; instead, the impl needs
13041286
// type parameters, which we can infer by unifying the provided `Self`
13051287
// with the substituted impl type.
13061288
// This also occurs for an enum variant on a type alias.
1307-
let ty = tcx.type_of(impl_def_id);
1308-
1309-
let impl_ty = self.instantiate_type_scheme(span, &substs, ty);
1289+
let impl_ty = self.normalize(span, tcx.bound_type_of(impl_def_id).subst(tcx, substs));
13101290
match self.at(&self.misc(span), self.param_env).eq(impl_ty, self_ty) {
13111291
Ok(ok) => self.register_infer_ok_obligations(ok),
13121292
Err(_) => {

‎compiler/rustc_hir_typeck/src/method/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
456456
// Instantiate late-bound regions and substitute the trait
457457
// parameters into the method type to get the actual method type.
458458
//
459-
// N.B., instantiate late-bound regions first so that
460-
// `instantiate_type_scheme` can normalize associated types that
461-
// may reference those regions.
459+
// N.B., instantiate late-bound regions before normalizing the
460+
// function signature so that normalization does not need to deal
461+
// with bound regions.
462462
let fn_sig = tcx.bound_fn_sig(def_id);
463463
let fn_sig = fn_sig.subst(self.tcx, substs);
464464
let fn_sig = self.replace_bound_vars_with_fresh_vars(span, infer::FnCall, fn_sig);

‎compiler/rustc_infer/src/infer/outlives/verify.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::infer::{GenericKind, VerifyBound};
55
use rustc_data_structures::sso::SsoHashSet;
66
use rustc_hir::def_id::DefId;
77
use rustc_middle::ty::GenericArg;
8-
use rustc_middle::ty::{self, EarlyBinder, OutlivesPredicate, SubstsRef, Ty, TyCtxt};
8+
use rustc_middle::ty::{self, OutlivesPredicate, SubstsRef, Ty, TyCtxt};
99

1010
use smallvec::smallvec;
1111

@@ -304,14 +304,13 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
304304
substs: SubstsRef<'tcx>,
305305
) -> impl Iterator<Item = ty::Region<'tcx>> {
306306
let tcx = self.tcx;
307-
let bounds = tcx.item_bounds(def_id);
308-
trace!("{:#?}", bounds);
307+
let bounds = tcx.bound_item_bounds(def_id);
308+
trace!("{:#?}", bounds.0);
309309
bounds
310-
.into_iter()
310+
.subst_iter(tcx, substs)
311311
.filter_map(|p| p.to_opt_type_outlives())
312312
.filter_map(|p| p.no_bound_vars())
313-
.map(|b| b.1)
314-
.map(move |r| EarlyBinder(r).subst(tcx, substs))
313+
.map(|OutlivesPredicate(_, r)| r)
315314
}
316315

317316
/// Searches through a predicate list for a predicate `T: 'a`.

‎compiler/rustc_middle/src/ty/util.rs

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
44
use crate::mir;
55
use crate::ty::layout::IntegerExt;
6+
use crate::ty::query::TyCtxtAt;
67
use crate::ty::{
78
self, DefIdTree, FallibleTypeFolder, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
89
TypeVisitable,
@@ -769,6 +770,12 @@ impl<'tcx> TyCtxt<'tcx> {
769770
}
770771
}
771772

773+
impl<'tcx> TyCtxtAt<'tcx> {
774+
pub fn bound_type_of(self, def_id: DefId) -> ty::EarlyBinder<Ty<'tcx>> {
775+
ty::EarlyBinder(self.type_of(def_id))
776+
}
777+
}
778+
772779
struct OpaqueTypeExpander<'tcx> {
773780
// Contains the DefIds of the opaque types that are currently being
774781
// expanded. When we expand an opaque type we insert the DefId of

0 commit comments

Comments
 (0)