Skip to content

Commit

Permalink
Merge pull request #2522 from 9rnsr/fix9565
Browse files Browse the repository at this point in the history
Issue 9565 - Index of static array should not print literal suffix
  • Loading branch information
WalterBright committed Sep 15, 2013
2 parents 2b7b591 + d1ab501 commit 7245666
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 13 deletions.
35 changes: 31 additions & 4 deletions src/expression.c
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,33 @@ void expToCBuffer(OutBuffer *buf, HdrGenState *hgs, Expression *e, PREC pr)
e->toCBuffer(buf, hgs);
}

void sizeToCBuffer(OutBuffer *buf, HdrGenState *hgs, Expression *e)
{
if (e->type == Type::tsize_t)
{
Expression *ex = (e->op == TOKcast ? ((CastExp *)e)->e1 : e);
ex = ex->optimize(WANTvalue);

dinteger_t uval = ex->op == TOKint64 ? ex->toInteger() : (dinteger_t)-1;
if ((sinteger_t)uval >= 0)
{
dinteger_t sizemax;
if (Target::ptrsize == 4)
sizemax = 0xFFFFFFFFUL;
else if (Target::ptrsize == 8)
sizemax = 0xFFFFFFFFFFFFFFFFULL;
else
assert(0);
if (uval <= sizemax && uval <= 0x7FFFFFFFFFFFFFFFULL)
{
buf->printf("%llu", uval);
return;
}
}
}
expToCBuffer(buf, hgs, e, PREC_assign);
}

/**************************************************
* Write out argument list to buf.
*/
Expand Down Expand Up @@ -10385,14 +10412,14 @@ void SliceExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
if (upr || lwr)
{
if (lwr)
expToCBuffer(buf, hgs, lwr, PREC_assign);
sizeToCBuffer(buf, hgs, lwr);
else
buf->writeByte('0');
buf->writestring("..");
if (upr)
expToCBuffer(buf, hgs, upr, PREC_assign);
sizeToCBuffer(buf, hgs, upr);
else
buf->writestring("length"); // BUG: should be array.length
buf->writestring("$");
}
buf->writeByte(']');
}
Expand Down Expand Up @@ -10895,7 +10922,7 @@ void IndexExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
expToCBuffer(buf, hgs, e1, PREC_primary);
buf->writeByte('[');
expToCBuffer(buf, hgs, e2, PREC_assign);
sizeToCBuffer(buf, hgs, e2);
buf->writeByte(']');
}

Expand Down
12 changes: 9 additions & 3 deletions src/mtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "hdrgen.h"

FuncDeclaration *hasThis(Scope *sc);
void sizeToCBuffer(OutBuffer *buf, HdrGenState *hgs, Expression *e);

#define LOGDOTEXP 0 // log ::dotExp()
#define LOGDEFAULTINIT 0 // log ::defaultInit()
Expand Down Expand Up @@ -4099,7 +4100,9 @@ void TypeSArray::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod)
return;
}
next->toCBuffer2(buf, hgs, this->mod);
buf->printf("[%s]", dim->toChars());
buf->writeByte('[');
sizeToCBuffer(buf, hgs, dim);
buf->writeByte(']');
}

Expression *TypeSArray::dotExp(Scope *sc, Expression *e, Identifier *ident, int flag)
Expand Down Expand Up @@ -9405,8 +9408,11 @@ void TypeSlice::toCBuffer2(OutBuffer *buf, HdrGenState *hgs, int mod)
}
next->toCBuffer2(buf, hgs, this->mod);

buf->printf("[%s .. ", lwr->toChars());
buf->printf("%s]", upr->toChars());
buf->writeByte('[');
sizeToCBuffer(buf, hgs, lwr);
buf->writestring(" .. ");
sizeToCBuffer(buf, hgs, upr);
buf->writeByte(']');
}

/***************************** TypeNull *****************************/
Expand Down
86 changes: 86 additions & 0 deletions test/compilable/test9565.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// REQUIRED_ARGS: -o-
// PERMUTE_ARGS:

template TypeTuple(T...) { alias TypeTuple = T; }

bool startsWith(string s, string m) { return s[0 .. m.length] == m; }

void main()
{
enum string castPrefix = "cast(" ~ size_t.stringof ~ ")";

// TypeSArray
static assert((int[10]).stringof == "int[10]", T.stringof);

int[] arr;

// IndexExp
{
// index == IntegerExp
static assert((arr[ 4 ]).stringof == "arr[4]");
static assert((arr[ 4U ]).stringof == "arr[4]");
static assert((arr[ 4L ]).stringof == "arr[4]");
static assert((arr[ 4LU]).stringof == "arr[4]");

// index == UAddExp
static assert((arr[+4 ]).stringof == "arr[4]");
static assert((arr[+4U ]).stringof == "arr[4]");
static assert((arr[+4L ]).stringof == "arr[4]");
static assert((arr[+4LU]).stringof == "arr[4]");

// index == NegExp
static assert((arr[-4 ]).stringof == "arr[" ~ castPrefix ~ "-4]");
static assert((arr[-4U ]).stringof == "arr[4294967292]");
static assert((arr[int.min] ).stringof == "arr[" ~ castPrefix ~ "-2147483648]");
static if (is(size_t == ulong))
{
static assert((arr[-4L ]).stringof == "arr[" ~ castPrefix ~ "-4L]");
static assert((arr[-4LU]).stringof == "arr[-4LU]");

// IntegerLiteral needs suffix if the value is greater than long.max
static assert((arr[long.max + 0]).stringof == "arr[9223372036854775807]");
static assert((arr[long.max + 1]).stringof == "arr[" ~ castPrefix ~ "(9223372036854775807L + 1L)]");
}

foreach (Int; TypeTuple!(byte, ubyte, short, ushort, int, uint, long, ulong))
{
enum Int p4 = +4;
enum string result1 = (arr[p4]).stringof;
static assert(result1 == "arr[4]");

enum string result2 = (arr[cast(Int)+4]).stringof;
static assert(result2 == "arr[4]");
}
foreach (Int; TypeTuple!(byte, short, int, long))
{
// keep "cast(Type)" in the string representation

enum Int m4 = -4;
static if (is(typeof({ size_t x = m4; })))
{
enum string result1 = (arr[m4]).stringof;
static assert(result1.startsWith("arr[" ~ castPrefix));
}
else
static assert(!__traits(compiles, arr[m4]));

enum string result2 = (arr[cast(Int)-4]).stringof;
static assert(result2.startsWith("arr[" ~ castPrefix));
}
}

// SliceExp
{
// lwr,upr == IntegerExp
static assert((arr[4 .. 8 ]).stringof == "arr[4..8]");
static assert((arr[4U .. 8U ]).stringof == "arr[4..8]");
static assert((arr[4L .. 8L ]).stringof == "arr[4..8]");
static assert((arr[4LU .. 8LU]).stringof == "arr[4..8]");

// lwr,upr == UAddExp
static assert((arr[+4 .. +8 ]).stringof == "arr[4..8]");
static assert((arr[+4U .. +8U ]).stringof == "arr[4..8]");
static assert((arr[+4L .. +8L ]).stringof == "arr[4..8]");
static assert((arr[+4LU .. +8LU]).stringof == "arr[4..8]");
}
}
4 changes: 2 additions & 2 deletions test/fail_compilation/diag7420.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ fail_compilation/diag7420.d(4): while evaluating: static assert(y == "abc
fail_compilation/diag7420.d(5): Error: static variable y cannot be read at compile time
fail_compilation/diag7420.d(5): while evaluating: static assert(cast(ubyte[])y != null)
fail_compilation/diag7420.d(6): Error: static variable y cannot be read at compile time
fail_compilation/diag7420.d(6): while evaluating: static assert(cast(int)y[0u] == 1)
fail_compilation/diag7420.d(6): while evaluating: static assert(cast(int)y[0] == 1)
fail_compilation/diag7420.d(7): Error: static variable y cannot be read at compile time
fail_compilation/diag7420.d(7): while evaluating: static assert(y[0u..1u].length == 1u)
fail_compilation/diag7420.d(7): while evaluating: static assert(y[0..1].length == 1u)
---
*/

Expand Down
3 changes: 1 addition & 2 deletions test/fail_compilation/diag8892.d
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// REQUIRED_ARGS: -m32
/*
TEST_OUTPUT:
---
fail_compilation/diag8892.d(15): Error: cannot implicitly convert expression (['A']) of type char[] to char[2u]
fail_compilation/diag8892.d(14): Error: cannot implicitly convert expression (['A']) of type char[] to char[2]
---
*/
struct Foo
Expand Down
4 changes: 2 additions & 2 deletions test/fail_compilation/fail10102.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
TEST_OUTPUT:
---
fail_compilation/fail10102.d(48): Error: variable fail10102.main.m initializer required for type NotNull!(int*)
fail_compilation/fail10102.d(49): Error: variable fail10102.main.a initializer required for type NotNull!(int*)[3u]
fail_compilation/fail10102.d(49): Error: variable fail10102.main.a initializer required for type NotNull!(int*)[3]
fail_compilation/fail10102.d(50): Error: default construction is disabled for type NotNull!(int*)
fail_compilation/fail10102.d(51): Error: field S.m must be initialized because it has no default constructor
---
*/
// REQUIRED_ARGS: -m32

struct NotNull(T)
{
T p;
Expand Down

0 comments on commit 7245666

Please sign in to comment.