|
| 1 | +use formality_types::{ |
| 2 | + collections::Set, |
| 3 | + grammar::{Lt, Parameter, RigidName, RigidTy, TraitRef, TyData, Variable, Wcs}, |
| 4 | + judgment_fn, set, |
| 5 | +}; |
| 6 | + |
| 7 | +use crate::{ |
| 8 | + decls::Decls, |
| 9 | + prove::{combinators::for_all, prove_normalize::prove_normalize, Constraints}, |
| 10 | + Env, |
| 11 | +}; |
| 12 | + |
| 13 | +// From https://rust-lang.github.io/rfcs/2451-re-rebalancing-coherence.html: |
| 14 | +// |
| 15 | +// Given `impl<P1..=Pn> Trait<T1..=Tn> for T0`, an impl is valid only if at least one of the following is true: |
| 16 | +// |
| 17 | +// - `Trait` is a local trait |
| 18 | +// - All of |
| 19 | +// - At least one of the types `T0..=Tn` must be a local type. Let `Ti` be the |
| 20 | +// first such type. |
| 21 | +// - No uncovered type parameters `P1..=Pn` may appear in `T0..Ti` (excluding |
| 22 | +// `Ti`) |
| 23 | +// |
| 24 | +// Given the following definitions: |
| 25 | +// |
| 26 | +// Covered Type: A type which appears as a parameter to another type. For example, |
| 27 | +// `T` is uncovered, but the `T` in `Vec<T>` is covered. This is only relevant for |
| 28 | +// type parameters. |
| 29 | +// |
| 30 | +// Fundamental Type: A type for which you cannot add a blanket impl backwards |
| 31 | +// compatibly. This includes `&`, `&mut`, and `Box`. Any time a type `T` is |
| 32 | +// considered local, `&T`, `&mut T`, and `Box<T>` are also considered local. |
| 33 | +// Fundamental types cannot cover other types. Any time the term "covered type" is |
| 34 | +// used, `&T`, `&mut T`, and `Box<T>` are not considered covered. |
| 35 | +// |
| 36 | +// Local Type: A struct, enum, or union which was defined in the current crate. |
| 37 | +// This is not affected by type parameters. `struct Foo` is considered local, but |
| 38 | +// `Vec<Foo>` is not. `LocalType<ForeignType>` is local. Type aliases and trait |
| 39 | +// aliases do not affect locality. |
| 40 | + |
| 41 | +/// True if `goal` may be remote. This is |
| 42 | +pub fn may_be_remote(decls: Decls, env: Env, assumptions: Wcs, goal: TraitRef) -> Set<Constraints> { |
| 43 | + assert!(env.is_in_coherence_mode()); |
| 44 | + |
| 45 | + let c = is_local_trait_ref(decls, &env, assumptions, goal); |
| 46 | + |
| 47 | + if c.is_empty() { |
| 48 | + // Cannot possibly be local, so always remote. |
| 49 | + return set![Constraints::none(env)]; |
| 50 | + } |
| 51 | + |
| 52 | + if c.iter().any(Constraints::unconditionally_true) { |
| 53 | + // If this is unconditionally known to be local, then it is never remote. |
| 54 | + return set![]; |
| 55 | + } |
| 56 | + |
| 57 | + // Otherwise it is ambiguous |
| 58 | + set![Constraints::none(env).ambiguous()] |
| 59 | +} |
| 60 | + |
| 61 | +judgment_fn! { |
| 62 | + pub fn is_local_trait_ref( |
| 63 | + decls: Decls, |
| 64 | + env: Env, |
| 65 | + assumptions: Wcs, |
| 66 | + goal: TraitRef, |
| 67 | + ) => Constraints { |
| 68 | + debug(goal, assumptions, env, decls) |
| 69 | + |
| 70 | + ( |
| 71 | + (if decls.is_local_trait_id(&goal.trait_id)) |
| 72 | + --- ("local trait") |
| 73 | + (is_local_trait_ref(decls, env, _assumptions, goal) => Constraints::none(env)) |
| 74 | + ) |
| 75 | + |
| 76 | + ( |
| 77 | + (0 .. goal.parameters.len() => i) |
| 78 | + (is_local_parameter(&decls, &env, &assumptions, &goal.parameters[i]) => c1) |
| 79 | + (let assumptions = c1.substitution().apply(&assumptions)) |
| 80 | + (let goal = c1.substitution().apply(&goal)) |
| 81 | + (for_all(&decls, &env, &assumptions, &goal.parameters[..i], ¬_downstream) => c2) |
| 82 | + --- ("local parameter") |
| 83 | + (is_local_trait_ref(decls, env, assumptions, goal) => c1.seq(c2)) |
| 84 | + ) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +judgment_fn! { |
| 89 | + fn not_downstream( |
| 90 | + decls: Decls, |
| 91 | + env: Env, |
| 92 | + assumptions: Wcs, |
| 93 | + parameter: Parameter, |
| 94 | + ) => Constraints { |
| 95 | + debug(parameter, assumptions, env, decls) |
| 96 | + |
| 97 | + ( |
| 98 | + (for_all(&decls, &env, &assumptions, ¶meters, ¬_downstream) => c) |
| 99 | + --- ("rigid") |
| 100 | + (not_downstream(decls, env, assumptions, RigidTy { name: _, parameters }) => c) |
| 101 | + ) |
| 102 | + |
| 103 | + ( |
| 104 | + --- ("lifetime") |
| 105 | + (not_downstream(_decls, env, _assumptions, _l: Lt) => Constraints::none(env)) |
| 106 | + ) |
| 107 | + |
| 108 | + ( |
| 109 | + --- ("type variable") |
| 110 | + (not_downstream(_decls, env, _assumptions, TyData::Variable(Variable::InferenceVar(_))) => Constraints::none(env).ambiguous()) |
| 111 | + ) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +judgment_fn! { |
| 116 | + fn is_local_parameter( |
| 117 | + decls: Decls, |
| 118 | + env: Env, |
| 119 | + assumptions: Wcs, |
| 120 | + goal: Parameter, |
| 121 | + ) => Constraints { |
| 122 | + debug(goal, assumptions, env, decls) |
| 123 | + |
| 124 | + assert(env.is_in_coherence_mode()) |
| 125 | + |
| 126 | + // If we can normalize `goal` to something else, check if that normalized form is local. |
| 127 | + ( |
| 128 | + (prove_normalize(&decls, env.with_coherence_mode(false), &assumptions, goal) => (c1, p)) |
| 129 | + (let c1 = c1.with_coherence_mode(true)) |
| 130 | + (let assumptions = c1.substitution().apply(&assumptions)) |
| 131 | + (is_local_parameter(&decls, c1.env(), assumptions, p) => c2) |
| 132 | + --- ("local parameter") |
| 133 | + (is_local_parameter(decls, env, assumptions, goal) => c1.seq(c2)) |
| 134 | + ) |
| 135 | + |
| 136 | + // Fundamental types are local if all their arguments are local. |
| 137 | + ( |
| 138 | + (if is_fundamental(&decls, &name)) |
| 139 | + (for_all(&decls, &env, &assumptions, ¶meters, &is_local_parameter) => c) // FIXME: should be `is_local_parameter` |
| 140 | + --- ("fundamental rigid type") |
| 141 | + (is_local_parameter(decls, env, assumptions, RigidTy { name, parameters }) => c) |
| 142 | + ) |
| 143 | + |
| 144 | + // ADTs are local if they were declared in this crate. |
| 145 | + ( |
| 146 | + (if decls.is_local_adt_id(&a)) |
| 147 | + --- ("local rigid type") |
| 148 | + (is_local_parameter(decls, env, _assumptions, RigidTy { name: RigidName::AdtId(a), parameters: _ }) => Constraints::none(env)) |
| 149 | + ) |
| 150 | + |
| 151 | + // Inference variables might or might not be local, depending on how they are instantiated. |
| 152 | + ( |
| 153 | + --- ("existential variable") |
| 154 | + (is_local_parameter(_decls, env, _assumptions, TyData::Variable(Variable::InferenceVar(_))) => Constraints::none(env).ambiguous()) |
| 155 | + ) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +fn is_fundamental(_decls: &Decls, name: &RigidName) -> bool { |
| 160 | + // From https://rust-lang.github.io/rfcs/2451-re-rebalancing-coherence.html: |
| 161 | + // |
| 162 | + // Fundamental Type: A type for which you cannot add a blanket impl backwards |
| 163 | + // compatibly. This includes `&`, `&mut`, and `Box`. Any time a type `T` is |
| 164 | + // considered local, `&T`, `&mut T`, and `Box<T>` are also considered local. |
| 165 | + // Fundamental types cannot cover other types. Any time the term "covered type" is |
| 166 | + // used, `&T`, `&mut T`, and `Box<T>` are not considered covered. |
| 167 | + |
| 168 | + match name { |
| 169 | + RigidName::AdtId(_) => false, // FIXME |
| 170 | + |
| 171 | + RigidName::Ref(_) => true, |
| 172 | + |
| 173 | + RigidName::ScalarId(_) |
| 174 | + | RigidName::Tuple(_) |
| 175 | + | RigidName::FnPtr(_) |
| 176 | + | RigidName::FnDef(_) => false, |
| 177 | + } |
| 178 | +} |
0 commit comments