Skip to content
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

Mutable fields with generics #157

Open
yannbolliger opened this issue May 6, 2021 · 1 comment
Open

Mutable fields with generics #157

yannbolliger opened this issue May 6, 2021 · 1 comment
Labels
enhancement Improvement to an existing feature extraction Feature or bug with the extraction phase imperative Feature or bug related to imperative features

Comments

@yannbolliger
Copy link
Collaborator

After solving #154, the next step is to make the same work for generic ADTs.

#[var(field)]
struct S {
  field: i32,
}
struct Gen<T>(T);

fn set_gen_field(mut g: Gen<S>) -> Gen<S> {
  g.0.field = 131415;
  g = Gen(S { field: 161718 });
  g
}

Ideas, roadmap

  • All fields should be @var without annotation
  • (try this first without generics)
  • Then also, all type parameters should be @mutable by default
  • However, as we only have consuming, mutable paramters, all methods can be @pure
  • and arguments with mutable type parameters as type, that are not mut in Rust, can swap the @mutable flag with @pure.
  • Otherwise, one might try to freshCopy all the type parameter-typed args.
@yannbolliger
Copy link
Collaborator Author

The above might work, as is suggested by these examples:

case class Ref[@mutable T](var x:T)
def add(a: @pure Ref[Int], b: Ref[Int]) = { b.x = a.x }
import stainless.lang._
import stainless.annotation._

object Test {
  case class Foo(var i: Int)

  @extern
  def mystery(x: Foo, @pure y: Foo) = {
    ()
  }

  def test = {
    val a = Foo(0)
    val b = Foo(1)

    mystery(a, b)

    assert(a.i == 0) // fails
    assert(b.i == 1) // passes
  }
}

@yannbolliger yannbolliger added enhancement Improvement to an existing feature extraction Feature or bug with the extraction phase imperative Feature or bug related to imperative features labels May 6, 2021
@yannbolliger yannbolliger self-assigned this May 11, 2021
@yannbolliger yannbolliger removed their assignment Jun 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvement to an existing feature extraction Feature or bug with the extraction phase imperative Feature or bug related to imperative features
Projects
None yet
1 participant