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

var &default using unit parameter gives empty value #1319

Closed
awelzel opened this issue Dec 9, 2022 · 4 comments · Fixed by #1320
Closed

var &default using unit parameter gives empty value #1319

awelzel opened this issue Dec 9, 2022 · 4 comments · Fixed by #1320
Labels
Diagnostics Enhancement Improvement of existing functionality

Comments

@awelzel
Copy link
Contributor

awelzel commented Dec 9, 2022

Hey - I'm probably doing something unusual/unexpected, but the behavior was surprising, so reporting here.

When defaulting to a unit parameter, the default stays empty (?) rather then the unit parameter's value at the time of instantiation. This is probably not something that should work, but maybe it should error out then.

root@7d84d55f9217:/src# cat default.spicy 
module Test;

public type X = unit {
  a: /[A-Z]/;
  b: Y(self.a);
  on %done {
    print "d", self.b.d;
    print "self", self;
  }
};

type Y = unit(a: bytes) {
  c: /[A-Z]/;
  var d: bytes &default=a;
};
root@7d84d55f9217:/src# echo -n "AB" | spicy-driver ./default.spicy 
d, 
self, [$a=b"A", $b=[$c=b"B", $d=b""]]

($d is empty instead of "A")

@bbannier
Copy link
Member

bbannier commented Dec 9, 2022

Currently unit variables require manual initialization in e.g., %init, so what you are trying here is unsupported. That being said, I wonder whether we shouldn't allow initializing them where they are declared, e.g., via simple assignment or via &default. Until we support that we should reject unsupported attributes on unit variable declarations.

@bbannier bbannier added Diagnostics Enhancement Improvement of existing functionality labels Dec 9, 2022
@awelzel
Copy link
Contributor Author

awelzel commented Dec 9, 2022

After more chatting with @bbannier , seems this is more about &default on vars rather than unit parameters per se:

root@7d84d55f9217:/src# cat really.spicy 
module Test;

public type X = unit {
  var vx: bytes &default="really?";
};

global gx:  X;
print gx, gx.vx;
root@7d84d55f9217:/src# echo -n "AB" | spicy-driver ./really.spicy 
[$vx=b""], 

@bbannier
Copy link
Member

bbannier commented Dec 9, 2022

I opened #1320 to add validation to reject code with unsupported unit variable attributes.

@rsmmr
Copy link
Member

rsmmr commented Dec 12, 2022

Approved the PR, but yeah, we should add support for &default and initialization for unit vars. I believe that would just need to be moved over to the HILTI struct that's being generated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Diagnostics Enhancement Improvement of existing functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants