Skip to content

add Movability to Generator #685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion book/src/clauses/well_known_traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Some common examples of auto traits are `Send` and `Sync`.
| slices | ⚬ | ⚬ | ⚬ | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ✅ |
| arrays | ✅ | ✅ | ✅ | ❌ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ✅ |
| closures | ✅ | ✅ | ✅ | ⚬ | ⚬ | ⚬ | ✅ | ⚬ | ⚬ | ✅ |
| generators | ⚬ | ⚬ | ❌ | ⚬ | ⚬ | ⚬ | ⚬ | | ❌ | |
| generators | ⚬ | ⚬ | ❌ | ⚬ | ⚬ | ⚬ | ⚬ | | ❌ | |
| gen. witness | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| opaque | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| foreign | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
Expand Down
10 changes: 10 additions & 0 deletions chalk-integration/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,3 +1236,13 @@ impl Lower for Safety {
}
}
}

impl Lower for Movability {
type Lowered = rust_ir::Movability;
fn lower(&self) -> Self::Lowered {
match self {
Movability::Static => rust_ir::Movability::Static,
Movability::Movable => rust_ir::Movability::Movable,
}
}
}
5 changes: 4 additions & 1 deletion chalk-integration/src/lowering/program_lowerer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,10 @@ impl ProgramLowerer {
Ok(GeneratorWitnessExistential { types: witnesses })
})?;

let generator_datum = GeneratorDatum { input_output };
let generator_datum = GeneratorDatum {
movability: defn.movability.lower(),
input_output,
};
let generator_witness = GeneratorWitnessDatum { inner_types };

let id = self.generator_ids[&defn.name.str];
Expand Down
6 changes: 6 additions & 0 deletions chalk-parse/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,16 @@ pub struct Variant {
pub name: Identifier,
pub fields: Vec<Field>,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Movability {
Static,
Movable,
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct GeneratorDefn {
pub name: Identifier,
pub movability: Movability,
pub variable_kinds: Vec<VariableKind>,
pub upvars: Vec<Ty>,
pub resume_ty: Ty,
Expand Down
8 changes: 7 additions & 1 deletion chalk-parse/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,19 @@ FnDefn: FnDefn = {
}
};

Movability: Movability = {
"static" => Movability::Static,
=> Movability::Movable
}

GeneratorDefn: GeneratorDefn = {
"generator" <n:Id> <p:Angle<VariableKind>> "[" "resume" "=" <resume:Ty> "," "yield" "=" <yield_ty:Ty> "]" <ret_ty:FnReturn?>
"generator" <m:Movability> <n:Id> <p:Angle<VariableKind>> "[" "resume" "=" <resume:Ty> "," "yield" "=" <yield_ty:Ty> "]" <ret_ty:FnReturn?>
"{"
"upvars" "[" <upvars:SemiColon<Ty>> "]"
"witnesses" <l:ExistsLifetimes?> "[" <witnesses:SemiColon<Ty>> "]"
"}" => GeneratorDefn {
name: n,
movability: m,
variable_kinds: p,
upvars: upvars,
witness_lifetimes: l.unwrap_or_default(),
Expand Down
23 changes: 17 additions & 6 deletions chalk-solve/src/clauses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use self::builder::ClauseBuilder;
use self::env_elaborator::elaborate_env_clauses;
use self::program_clauses::ToProgramClauses;
use crate::goal_builder::GoalBuilder;
use crate::rust_ir::{Movability, WellKnownTrait};
use crate::split::Split;
use crate::RustIrDatabase;
use chalk_ir::cast::{Cast, Caster};
Expand Down Expand Up @@ -149,12 +150,7 @@ pub fn push_auto_trait_impls<I: Interner>(
match ty {
// function-types implement auto traits unconditionally
TyKind::Function(_) => {
let auto_trait_ref = TraitRef {
trait_id: auto_trait_id,
substitution: Substitution::from1(interner, ty.clone().intern(interner)),
};

builder.push_fact(auto_trait_ref);
builder.push_fact(consequence);
Ok(())
}
TyKind::InferenceVar(_, _) | TyKind::BoundVar(_) => Err(Floundered),
Expand All @@ -173,6 +169,21 @@ pub fn push_auto_trait_impls<I: Interner>(
});
Ok(())
}
TyKind::Generator(generator_id, _) => {
if Some(auto_trait_id) == builder.db.well_known_trait_id(WellKnownTrait::Unpin) {
match builder.db.generator_datum(*generator_id).movability {
// immovable generators are never `Unpin`
Movability::Static => (),
// movable generators are always `Unpin`
Movability::Movable => builder.push_fact(consequence),
}
} else {
// if trait is not `Unpin`, use regular auto trait clause
let conditions = constituent_types(builder.db, ty).into_iter().map(mk_ref);
builder.push_clause(consequence, conditions);
}
Ok(())
}

TyKind::GeneratorWitness(generator_id, _) => {
push_auto_trait_impls_generator_witness(builder, auto_trait_id, *generator_id);
Expand Down
13 changes: 12 additions & 1 deletion chalk-solve/src/rust_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,12 +638,23 @@ pub struct OpaqueTyDatumBound<I: Interner> {
pub where_clauses: Binders<Vec<QuantifiedWhereClause<I>>>,
}

// The movability of a generator: whether a generator contains self-references,
// causing it to be !Unpin
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Movability {
Static,
Movable,
}
chalk_ir::copy_fold!(Movability);

/// Represents a generator type.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Fold, HasInterner)]
pub struct GeneratorDatum<I: Interner> {
// Can the generator be moved (is Unpin or not)
pub movability: Movability,
/// All of the nested types for this generator. The `Binder`
/// represents the types and lifetimes that this generator is generic over -
/// this behaves in the same way as `AdtDatun.binders`
/// this behaves in the same way as `AdtDatum.binders`
pub input_output: Binders<GeneratorInputOutputDatum<I>>,
}

Expand Down
9 changes: 8 additions & 1 deletion tests/test/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ fn generator_test() {
witnesses exists<'a, 'b> [SendAnyLifetime<'a, 'b, T>; u8]
}


generator not_send_resume_yield<>[resume = NotSend, yield = NotSend] {
upvars []
witnesses []
Expand Down Expand Up @@ -85,5 +84,13 @@ fn generator_test() {
} yields {
"Unique; substitution [], lifetime constraints []"
}

goal {
forall<T> {
send_any_lifetime<T>: Send
}
} yields {
"No possible solution"
}
}
}
44 changes: 44 additions & 0 deletions tests/test/unpin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,47 @@ fn unpin_overwrite() {
}
}
}

#[test]
fn generator_unpin() {
test! {
program {
#[auto] #[lang(unpin)] trait Unpin { }
struct A { }
impl !Unpin for A {}

generator static static_gen<>[resume = (), yield = ()] {
upvars []
witnesses []
}

generator movable_gen<>[resume = (), yield = ()] {
upvars []
witnesses []
}

generator movable_with_pin<>[resume = (), yield = ()] {
upvars [A]
witnesses []
}
}

goal {
static_gen: Unpin
} yields {
"No possible solution"
}

goal {
movable_gen: Unpin
} yields {
"Unique"
}

goal {
movable_with_pin: Unpin
} yields {
"Unique"
}
}
}