Skip to content

Commit d0ec8ea

Browse files
committed
Implement RFC 2056 - trivial constraints
1 parent 0b17da2 commit d0ec8ea

File tree

2 files changed

+28
-46
lines changed

2 files changed

+28
-46
lines changed

src/librustc/traits/mod.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -641,17 +641,8 @@ pub fn normalize_param_env_or_error<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
641641

642642
let predicates: Vec<_> =
643643
util::elaborate_predicates(tcx, unnormalized_env.caller_bounds.to_vec())
644-
.filter(|p| !p.is_global()) // (*)
645644
.collect();
646645

647-
// (*) Any predicate like `i32: Trait<u32>` or whatever doesn't
648-
// need to be in the *environment* to be proven, so screen those
649-
// out. This is important for the soundness of inter-fn
650-
// caching. Note though that we should probably check that these
651-
// predicates hold at the point where the environment is
652-
// constructed, but I am not currently doing so out of laziness.
653-
// -nmatsakis
654-
655646
debug!("normalize_param_env_or_error: elaborated-predicates={:?}",
656647
predicates);
657648

src/librustc/traits/select.rs

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,6 @@ enum BuiltinImplConditions<'tcx> {
305305
/// There is no built-in impl. There may be some other
306306
/// candidate (a where-clause or user-defined impl).
307307
None,
308-
/// There is *no* impl for this, builtin or not. Ignore
309-
/// all where-clauses.
310-
Never,
311308
/// It is unknown whether there is an impl.
312309
Ambiguous
313310
}
@@ -781,13 +778,13 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
781778
mut obligation: TraitObligation<'tcx>)
782779
-> Result<EvaluationResult, OverflowError>
783780
{
784-
debug!("evaluate_trait_predicate_recursively({:?})",
785-
obligation);
781+
debug!("evaluate_trait_predicate_recursively({:?})", obligation);
786782

787-
if !self.intercrate.is_some() && obligation.is_global() {
788-
// If a param env is consistent, global obligations do not depend on its particular
789-
// value in order to work, so we can clear out the param env and get better
790-
// caching. (If the current param env is inconsistent, we don't care what happens).
783+
if self.intercrate.is_none() && obligation.is_global()
784+
&& obligation.param_env.caller_bounds.iter().all(|bound| bound.needs_subst()) {
785+
// If a param env has no global bounds, global obligations do not
786+
// depend on its particular value in order to work, so we can clear
787+
// out the param env and get better caching.
791788
debug!("evaluate_trait_predicate_recursively({:?}) - in global", obligation);
792789
obligation.param_env = obligation.param_env.without_caller_bounds();
793790
}
@@ -1451,22 +1448,22 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
14511448
let sized_conditions = self.sized_conditions(obligation);
14521449
self.assemble_builtin_bound_candidates(sized_conditions,
14531450
&mut candidates)?;
1454-
} else if lang_items.unsize_trait() == Some(def_id) {
1455-
self.assemble_candidates_for_unsizing(obligation, &mut candidates);
1456-
} else {
1457-
if lang_items.clone_trait() == Some(def_id) {
1458-
// Same builtin conditions as `Copy`, i.e. every type which has builtin support
1459-
// for `Copy` also has builtin support for `Clone`, + tuples and arrays of `Clone`
1460-
// types have builtin support for `Clone`.
1461-
let clone_conditions = self.copy_clone_conditions(obligation);
1462-
self.assemble_builtin_bound_candidates(clone_conditions, &mut candidates)?;
1463-
}
1464-
1465-
self.assemble_generator_candidates(obligation, &mut candidates)?;
1466-
self.assemble_closure_candidates(obligation, &mut candidates)?;
1467-
self.assemble_fn_pointer_candidates(obligation, &mut candidates)?;
1468-
self.assemble_candidates_from_impls(obligation, &mut candidates)?;
1469-
self.assemble_candidates_from_object_ty(obligation, &mut candidates);
1451+
} else if lang_items.unsize_trait() == Some(def_id) {
1452+
self.assemble_candidates_for_unsizing(obligation, &mut candidates);
1453+
} else {
1454+
if lang_items.clone_trait() == Some(def_id) {
1455+
// Same builtin conditions as `Copy`, i.e. every type which has builtin support
1456+
// for `Copy` also has builtin support for `Clone`, + tuples and arrays of `Clone`
1457+
// types have builtin support for `Clone`.
1458+
let clone_conditions = self.copy_clone_conditions(obligation);
1459+
self.assemble_builtin_bound_candidates(clone_conditions, &mut candidates)?;
1460+
}
1461+
1462+
self.assemble_generator_candidates(obligation, &mut candidates)?;
1463+
self.assemble_closure_candidates(obligation, &mut candidates)?;
1464+
self.assemble_fn_pointer_candidates(obligation, &mut candidates)?;
1465+
self.assemble_candidates_from_impls(obligation, &mut candidates)?;
1466+
self.assemble_candidates_from_object_ty(obligation, &mut candidates);
14701467
}
14711468

14721469
self.assemble_candidates_from_projected_tys(obligation, &mut candidates);
@@ -2081,13 +2078,8 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
20812078
// BUILTIN BOUNDS
20822079
//
20832080
// These cover the traits that are built-in to the language
2084-
// itself. This includes `Copy` and `Sized` for sure. For the
2085-
// moment, it also includes `Send` / `Sync` and a few others, but
2086-
// those will hopefully change to library-defined traits in the
2087-
// future.
2081+
// itself: `Copy`, `Clone` and `Sized`.
20882082

2089-
// HACK: if this returns an error, selection exits without considering
2090-
// other impls.
20912083
fn assemble_builtin_bound_candidates<'o>(&mut self,
20922084
conditions: BuiltinImplConditions<'tcx>,
20932085
candidates: &mut SelectionCandidateSet<'tcx>)
@@ -2106,14 +2098,13 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
21062098
debug!("assemble_builtin_bound_candidates: ambiguous builtin");
21072099
Ok(candidates.ambiguous = true)
21082100
}
2109-
BuiltinImplConditions::Never => { Err(Unimplemented) }
21102101
}
21112102
}
21122103

21132104
fn sized_conditions(&mut self, obligation: &TraitObligation<'tcx>)
21142105
-> BuiltinImplConditions<'tcx>
21152106
{
2116-
use self::BuiltinImplConditions::{Ambiguous, None, Never, Where};
2107+
use self::BuiltinImplConditions::{Ambiguous, None, Where};
21172108

21182109
// NOTE: binder moved to (*)
21192110
let self_ty = self.infcx.shallow_resolve(
@@ -2130,7 +2121,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
21302121
Where(ty::Binder::dummy(Vec::new()))
21312122
}
21322123

2133-
ty::TyStr | ty::TySlice(_) | ty::TyDynamic(..) | ty::TyForeign(..) => Never,
2124+
ty::TyStr | ty::TySlice(_) | ty::TyDynamic(..) | ty::TyForeign(..) => None,
21342125

21352126
ty::TyTuple(tys) => {
21362127
Where(ty::Binder::bind(tys.last().into_iter().cloned().collect()))
@@ -2164,7 +2155,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
21642155
let self_ty = self.infcx.shallow_resolve(
21652156
obligation.predicate.skip_binder().self_ty());
21662157

2167-
use self::BuiltinImplConditions::{Ambiguous, None, Never, Where};
2158+
use self::BuiltinImplConditions::{Ambiguous, None, Where};
21682159

21692160
match self_ty.sty {
21702161
ty::TyInfer(ty::IntVar(_)) | ty::TyInfer(ty::FloatVar(_)) |
@@ -2182,7 +2173,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
21822173
ty::TyDynamic(..) | ty::TyStr | ty::TySlice(..) |
21832174
ty::TyGenerator(..) | ty::TyGeneratorWitness(..) | ty::TyForeign(..) |
21842175
ty::TyRef(_, _, hir::MutMutable) => {
2185-
Never
2176+
None
21862177
}
21872178

21882179
ty::TyArray(element_ty, _) => {
@@ -2202,7 +2193,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
22022193
if is_copy_trait || is_clone_trait {
22032194
Where(ty::Binder::bind(substs.upvar_tys(def_id, self.tcx()).collect()))
22042195
} else {
2205-
Never
2196+
None
22062197
}
22072198
}
22082199

0 commit comments

Comments
 (0)