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

(Relevant to Enum:) Uppercase identifiers assumed to be stable identifiers in extractor/unapply context #694

Open
sdtwigg opened this issue Apr 15, 2016 · 0 comments

Comments

@sdtwigg
Copy link
Contributor

sdtwigg commented Apr 15, 2016

Any time you use scala extractor syntax (sometimes called doing an unapply), identifiers that start with an uppercase letter are assumed to be stable (already bound to a value) identifiers. identifiers that do not are assumed to be unbound and thus able to be set as part of the extraction.

Looking specifically at the Enum case (which I'm going to unroll into a List of Int so you can play with these examples in the REPL more easily)...

val a :: b :: c :: d :: Nil = List(1, 2, 3, 4)

After this a == 1 && b == 2 && c == 3 && d == 4 as expected.

In contrast, the following will complain that A, B, C, and D values do not exist:

val A :: B :: C :: D :: Nil = List(1, 2, 3, 4)

scalac expected that something like this had happened:

val A = 1; val B = 2; val C = 3; val D = 4;
val A :: B :: C :: D :: x :: Nil = List(1, 2, 3, 4, 6)

where now the only new identifier is x == 6.
(Note: val A :: B :: C :: D :: Nil = List(0, 0, 0, 0) will fail with a scala.MatchError)

The takeaway is that, this sort of code using Chisel will not compile as expected:

val STATE_A :: STATE_B :: STATE_C :: Nil = Enum(UInt(), 3)

since it expects already existing STATE_A, STATE_B, STATE_C identifiers.

The Enum documentation should be amended to explicitly note this issue (as Verilog developers are very, very used to using uppercase identifiers as constant/localparam definitions).

PS: Another other time that extractor/unapply syntax comes up is in match-case, e.g. the following is functionally identical to the above expression:

val x = List(1,2,3,4,6) match {case A :: B :: C :: D :: x :: Nil => x}

PPS: This also applies to the Enum in Chisel3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant