Skip to content

Commit 5a5c265

Browse files
committed
refactor common logic into ParameterEnvironment::and()
1 parent 194d4bc commit 5a5c265

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

src/librustc/ty/mod.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,10 +1259,33 @@ pub struct ParameterEnvironment<'tcx> {
12591259
}
12601260

12611261
impl<'tcx> ParameterEnvironment<'tcx> {
1262-
pub fn and<T>(self, value: T) -> ParameterEnvironmentAnd<'tcx, T> {
1263-
ParameterEnvironmentAnd {
1264-
param_env: self,
1265-
value: value,
1262+
/// Creates a suitable environment in which to perform trait
1263+
/// queries on the given value. This will either be `self` *or*
1264+
/// the empty environment, depending on whether `value` references
1265+
/// type parameters that are in scope. (If it doesn't, then any
1266+
/// judgements should be completely independent of the context,
1267+
/// and hence we can safely use the empty environment so as to
1268+
/// enable more sharing across functions.)
1269+
///
1270+
/// NB: This is a mildly dubious thing to do, in that a function
1271+
/// (or other environment) might have wacky where-clauses like
1272+
/// `where Box<u32>: Copy`, which are clearly never
1273+
/// satisfiable. The code will at present ignore these,
1274+
/// effectively, when type-checking the body of said
1275+
/// function. This preserves existing behavior in any
1276+
/// case. --nmatsakis
1277+
pub fn and<T: TypeFoldable<'tcx>>(self, value: T) -> ParameterEnvironmentAnd<'tcx, T> {
1278+
assert!(!value.needs_infer());
1279+
if value.has_param_types() || value.has_self_ty() {
1280+
ParameterEnvironmentAnd {
1281+
param_env: self,
1282+
value: value,
1283+
}
1284+
} else {
1285+
ParameterEnvironmentAnd {
1286+
param_env: ParameterEnvironment::empty(),
1287+
value: value,
1288+
}
12661289
}
12671290
}
12681291
}

src/librustc/ty/util.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -724,35 +724,23 @@ impl<'a, 'tcx> ty::TyS<'tcx> {
724724
param_env: ParameterEnvironment<'tcx>,
725725
span: Span)
726726
-> bool {
727-
if self.has_param_types() || self.has_self_ty() {
728-
!tcx.at(span).is_copy_raw(param_env.and(self))
729-
} else {
730-
!tcx.is_copy_raw(ParameterEnvironment::empty().and(self))
731-
}
727+
!tcx.at(span).is_copy_raw(param_env.and(self))
732728
}
733729

734730
pub fn is_sized(&'tcx self,
735731
tcx: TyCtxt<'a, 'tcx, 'tcx>,
736732
param_env: ParameterEnvironment<'tcx>,
737733
span: Span)-> bool
738734
{
739-
if self.has_param_types() || self.has_self_ty() {
740-
tcx.at(span).is_sized_raw(param_env.and(self))
741-
} else {
742-
tcx.is_sized_raw(ParameterEnvironment::empty().and(self))
743-
}
735+
tcx.at(span).is_sized_raw(param_env.and(self))
744736
}
745737

746738
pub fn is_freeze(&'tcx self,
747739
tcx: TyCtxt<'a, 'tcx, 'tcx>,
748740
param_env: ParameterEnvironment<'tcx>,
749741
span: Span)-> bool
750742
{
751-
if self.has_param_types() || self.has_self_ty() {
752-
tcx.at(span).is_freeze_raw(param_env.and(self))
753-
} else {
754-
tcx.is_freeze_raw(ParameterEnvironment::empty().and(self))
755-
}
743+
tcx.at(span).is_freeze_raw(param_env.and(self))
756744
}
757745

758746
/// If `ty.needs_drop(...)` returns `true`, then `ty` is definitely

0 commit comments

Comments
 (0)