Skip to content

Commit b70e543

Browse files
committed
internal/core/runtime: new attribute name for extern
Right now, we only allow one type of extern attribute in a file. At the file level, we define @extern(kind). Fields within the file can then be associated with an @extern attribute that is interpreted as defined by kind. This approach may work for lower-level functionality like support for WASM, but it seems a bit unintuitive for embed. Instead, we suggest that after a file-level @extern(kind) declaration the field attributes take the form @kind(). This is what is implemented here. This has the additional benefit that we could more easily allow different types of extern fields within a single file. Note that the original reason to reuse @extern for field attributes was to avoid a proliferation of attributes. This namespace encrouching is still a bit mitigated by the @extern(kind) attribute. In the future we can find a different mechanism to define attributes scoped by domain. Issue #2031 Signed-off-by: Marcel van Lohuizen <[email protected]> Change-Id: I28b1fdd0f0a85c46a544f71bbff40a7772e60873 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1196717 Reviewed-by: Daniel Martí <[email protected]> Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent a1c1cd7 commit b70e543

File tree

8 files changed

+59
-28
lines changed

8 files changed

+59
-28
lines changed

internal/core/runtime/extern.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,22 +245,23 @@ func (d *externDecorator) markExternFieldAttr(kind string, decls []ast.Decl) (er
245245

246246
case *ast.Attribute:
247247
key, body := x.Split()
248-
if key != "extern" {
248+
// Support old-style and new-style extern attributes.
249+
if key != "extern" && key != kind {
249250
break
250251
}
251252

252253
lastField := len(fieldStack) - 1
253254
if lastField < 0 {
254255
errs = errors.Append(errs, errors.Newf(x.Pos(),
255-
"extern attribute not associated with field"))
256+
"@%s attribute not associated with field", kind))
256257
return true
257258
}
258259

259260
f := fieldStack[lastField]
260261

261262
if _, ok := d.fields[f]; ok {
262263
errs = errors.Append(errs, errors.Newf(x.Pos(),
263-
"duplicate extern attributes"))
264+
"duplicate @%s attributes", kind))
264265
return true
265266
}
266267

internal/core/runtime/testdata/basic.txtar

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
package foo
66

77

8-
Foo: _ @extern(file1.xx, abi=c, sig="func(int)int")
8+
Foo: _ @test(file1.xx, abi=c, sig="func(int)int")
99

10-
Rename: _ @extern(file1.xx, name=Emaner, abi=c, sig="func(int)int")
10+
Rename: _ @test(file1.xx, name=Emaner, abi=c, sig="func(int)int")
1111

1212
-- file2.cue --
1313
@extern("test")
@@ -17,7 +17,7 @@ package foo
1717

1818
Bar: {
1919
@other()
20-
@extern(file2.xx, abi=c, sig="func(int)int")
20+
@test(file2.xx, abi=c, sig="func(int)int")
2121
_
2222
}
2323

internal/core/runtime/testdata/compile.txtar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
package ok
55

6-
foo: _ @extern("file.xx", fail)
6+
foo: _ @test("file.xx", fail)
77
-- out/extern --
88
can't load from external module: TEST: fail compilation:
99
./compile.cue:5:8

internal/core/runtime/testdata/errors.txtar

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,30 @@ package foo
1313

1414
package foo
1515

16-
Fn1: _ @extern("file1.xx" abi sig)
16+
Fn1: _ @test("file1.xx" abi sig)
1717

1818
-- empty_extern.cue --
1919
@extern()
2020

2121
package foo
2222

23-
Fn2: _ @extern("file1.xx" abi sig)
23+
Fn2: _ @test("file1.xx" abi sig)
2424

2525

2626
-- unknown_interpreter.cue --
2727
@extern("wazem")
2828

2929
package foo
3030

31-
Fn3: _ @extern("file1.xx" abi sig)
31+
Fn3: _ @wazem("file1.xx" abi sig)
3232

3333
-- double_extern_a.cue --
3434
@extern("test")
3535
@extern("test")
3636

3737
package foo
3838

39-
Fn4a: _ @extern("file1.xx")
39+
Fn4a: _ @test("file1.xx")
4040

4141
-- double_extern_b.cue --
4242
@extern("test")
@@ -45,14 +45,14 @@ Fn4a: _ @extern("file1.xx")
4545

4646
package foo
4747

48-
Fn4b: _ @extern("file1.xx")
48+
Fn4b: _ @test("file1.xx")
4949

5050
-- package_attr.cue --
5151
@extern("test")
5252

5353
package foo
5454

55-
@extern("file1.xx")
55+
@test("file1.xx")
5656
Fn5: _
5757

5858
-- duplicate.cue --
@@ -61,39 +61,39 @@ Fn5: _
6161
package foo
6262

6363

64-
Fn6: _ @extern("file1.xx",sig=func(int)int) @extern("file1.xx", sig=func(int)bool)
64+
Fn6: _ @test("file1.xx",sig=func(int)int) @test("file1.xx", sig=func(int)bool)
6565

6666
Fn7: {
67-
@extern("file1.xx",sig=func(int)int)
67+
@test("file1.xx",sig=func(int)int)
6868
_
69-
} @extern("file1.xx", sig=func(int)bool)
69+
} @test("file1.xx", sig=func(int)bool)
7070

7171
-- non_ident.cue --
7272
@extern("test")
7373

7474
package foo
7575

7676

77-
"a-b": _ @extern("file1.xx",sig=func(int)int)
77+
"a-b": _ @test("file1.xx",sig=func(int)int)
7878

79-
[string]: _ @extern("file1.xx",sig=func(int)int)
79+
[string]: _ @test("file1.xx",sig=func(int)int)
8080

8181
-- late_extern.cue --
8282
package foo
8383

8484
@extern("test")
8585

8686

87-
Foo: _ @extern(file1.xx, abi=c, sig="func(int)int")
87+
Foo: _ @test(file1.xx, abi=c, sig="func(int)int")
8888

8989
-- out/extern --
9090
only one file-level extern attribute allowed per file:
9191
./double_extern_a.cue:2:1
9292
only one file-level extern attribute allowed per file:
9393
./double_extern_b.cue:2:1
94-
duplicate extern attributes:
95-
./duplicate.cue:6:45
96-
duplicate extern attributes:
94+
duplicate @test attributes:
95+
./duplicate.cue:6:43
96+
duplicate @test attributes:
9797
./duplicate.cue:11:3
9898
interpreter name must be non-empty:
9999
./empty_extern.cue:1:1
@@ -105,7 +105,7 @@ can only define functions for fields with identifier names, found "a-b":
105105
./non_ident.cue:6:10
106106
can only define functions for fields with identifier names, found [string]:
107107
./non_ident.cue:8:13
108-
extern attribute not associated with field:
108+
@test attribute not associated with field:
109109
./package_attr.cue:5:1
110110
no interpreter defined for "wazem":
111111
./unknown_interpreter.cue:1:1

internal/core/runtime/testdata/failinit.txtar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
package failinit
77

88

9-
foo: _ @extern("file.xx")
9+
foo: _ @test("file.xx")
1010

1111
-- out/extern --
1212
TEST: fail initialization
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
-- cue.mod/modules.cue --
2+
-- file1.cue --
3+
@extern("test")
4+
5+
package foo
6+
7+
8+
Foo: _ @extern(file1.xx, abi=c, sig="func(int)int")
9+
10+
Rename: _ @extern(file1.xx, name=Emaner, abi=c, sig="func(int)int")
11+
12+
-- file2.cue --
13+
@extern("test")
14+
15+
package foo
16+
17+
18+
Bar: {
19+
@other()
20+
@extern(file2.xx, abi=c, sig="func(int)int")
21+
_
22+
}
23+
24+
-- extern/out --
25+
-- out/extern --
26+
{
27+
Foo: implFoo1
28+
Bar: implBar2
29+
Rename: implEmaner1
30+
}

internal/core/runtime/testdata/nested.txtar

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
package foo
66

77

8-
a: Foo: _ @extern(file1.xx, abi=c, sig="func(int)int")
8+
a: Foo: _ @test(file1.xx, abi=c, sig="func(int)int")
99

10-
a: Rename: _ @extern(file1.xx, name=Emaner, abi=c, sig="func(int)int")
10+
a: Rename: _ @test(file1.xx, name=Emaner, abi=c, sig="func(int)int")
1111

1212
-- file2.cue --
1313
@extern("test")
@@ -17,7 +17,7 @@ package foo
1717

1818
a: foo: Bar: {
1919
@other()
20-
@extern(file2.xx, abi=c, sig="func(int)int")
20+
@test(file2.xx, abi=c, sig="func(int)int")
2121
_
2222
}
2323

internal/core/runtime/testdata/no_top_extern.txtar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
package foo
99

1010

11-
Foo: _ @extern(file.xx, abi=c, sig="func(int)int")
11+
Foo: _ @test(file.xx, abi=c, sig="func(int)int")
1212

1313
-- extern/out --
1414
-- out/extern/config --

0 commit comments

Comments
 (0)