From 1b1ba197ec705b422203ecee04c22e464329bdcc Mon Sep 17 00:00:00 2001 From: Araq Date: Mon, 13 Jan 2020 11:34:04 +0100 Subject: [PATCH 1/2] fixes #13122 --- compiler/ast.nim | 4 +++- compiler/pragmas.nim | 6 ++---- tests/misc/talignas.nim | 15 ++++++++++++--- tests/misc/tillegalalignas.nim | 2 +- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/compiler/ast.nim b/compiler/ast.nim index 99733ef3fc88..2cdf40a8f24c 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -826,7 +826,7 @@ type of skLet, skVar, skField, skForVar: guard*: PSym bitsize*: int - alignment*: int # for alignas(X) expressions + alignment*: int # for alignment else: nil magic*: TMagic typ*: PType @@ -1398,6 +1398,8 @@ proc copySym*(s: PSym): PSym = result.annex = s.annex # BUGFIX if result.kind in {skVar, skLet, skField}: result.guard = s.guard + result.bitsize = s.bitsize + result.alignment = s.alignment proc createModuleAlias*(s: PSym, newIdent: PIdent, info: TLineInfo; options: TOptions): PSym = diff --git a/compiler/pragmas.nim b/compiler/pragmas.nim index 1802493f4115..0004fe94b024 100644 --- a/compiler/pragmas.nim +++ b/compiler/pragmas.nim @@ -819,12 +819,10 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: var int, localError(c.config, it.info, "size may only be 1, 2, 4 or 8") of wAlign: let alignment = expectIntLit(c, it) - if alignment == 0: - discard - elif isPowerOfTwo(alignment): + if isPowerOfTwo(alignment) and alignment > 0: sym.alignment = max(sym.alignment, alignment) else: - localError(c.config, it.info, "power of two or 0 expected") + localError(c.config, it.info, "power of two expected") of wNodecl: noVal(c, it) incl(sym.loc.flags, lfNoDecl) diff --git a/tests/misc/talignas.nim b/tests/misc/talignas.nim index 5d5250309c38..e0503cc70b8b 100644 --- a/tests/misc/talignas.nim +++ b/tests/misc/talignas.nim @@ -31,9 +31,9 @@ proc foobar() = doAssert (cast[uint](addr(toplevel3)) and 31) == 0 # test multiple align expressions - var mylocal1 {.align(0), align(128), align(32).}: int = 123 - var mylocal2 {.align(128), align(0), align(32).}: int = 123 - var mylocal3 {.align(0), align(32), align(128).}: int = 123 + var mylocal1 {.align(128), align(32).}: int = 123 + var mylocal2 {.align(128), align(32).}: int = 123 + var mylocal3 {.align(32), align(128).}: int = 123 doAssert (cast[uint](addr(mylocal1)) and 127) == 0 doAssert (cast[uint](addr(mylocal2)) and 127) == 0 @@ -42,3 +42,12 @@ proc foobar() = echo "align ok" foobar() + +# bug #13122 + +type Bug[T] = object + bug{.align:64.}: T + sideffect{.align:64.}: int + +var bug: Bug[int] +doAssert sizeof(bug) == 128, "Oops my size is " & $sizeof(bug) # 16 diff --git a/tests/misc/tillegalalignas.nim b/tests/misc/tillegalalignas.nim index 56721f2eaa50..c4a95f11b2e1 100644 --- a/tests/misc/tillegalalignas.nim +++ b/tests/misc/tillegalalignas.nim @@ -1,6 +1,6 @@ discard """ cmd: "nim check $options $file" -errormsg: "power of two or 0 expected" +errormsg: "power of two expected" """ proc foobar() = From 79d3b5c293ed82bf313c1760582972639c0daba0 Mon Sep 17 00:00:00 2001 From: Araq Date: Mon, 13 Jan 2020 11:35:47 +0100 Subject: [PATCH 2/2] moved tests to where they belong --- tests/{misc => align}/globalalignas.nim | 0 tests/{misc/talignas.nim => align/talign.nim} | 0 tests/{misc/tillegalalignas.nim => align/tillegalalign.nim} | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename tests/{misc => align}/globalalignas.nim (100%) rename tests/{misc/talignas.nim => align/talign.nim} (100%) rename tests/{misc/tillegalalignas.nim => align/tillegalalign.nim} (100%) diff --git a/tests/misc/globalalignas.nim b/tests/align/globalalignas.nim similarity index 100% rename from tests/misc/globalalignas.nim rename to tests/align/globalalignas.nim diff --git a/tests/misc/talignas.nim b/tests/align/talign.nim similarity index 100% rename from tests/misc/talignas.nim rename to tests/align/talign.nim diff --git a/tests/misc/tillegalalignas.nim b/tests/align/tillegalalign.nim similarity index 100% rename from tests/misc/tillegalalignas.nim rename to tests/align/tillegalalign.nim