Thread overview
[Issue 196] New: Static assertion involving typedef's base type fails strangely
Jun 15, 2006
d-bugmail
Jul 07, 2006
Thomas Kuehne
Jan 01, 2007
d-bugmail
Jun 23, 2008
d-bugmail
June 15, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=196

           Summary: Static assertion involving typedef's base type fails
                    strangely
           Product: D
           Version: 0.160
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: diagnostic, rejects-valid
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: deewiant@gmail.com


I need to static assert that a typedef's base type is equal to int.

I tried the code below, but it fails with the message "static assert  (is(int == int)) is false". If this is not meant to work, this is a bug in the sense that the error message makes no sense; if this is meant to work, it's a bug in the sense that it doesn't work.

In the former case, feel free to change the priority, severity, and keywords to match: I'm currently assuming that this should work.

--
typedef int x;

static assert (is (typeof(typeid(x).base) == int));
--

Also, there's an extra space between "assert" and "(is" in the error message.


-- 

July 07, 2006
d-bugmail@puremagic.com schrieb am 2006-06-15:
> http://d.puremagic.com/issues/show_bug.cgi?id=196

> I need to static assert that a typedef's base type is equal to int.
>
> I tried the code below, but it fails with the message "static assert  (is(int
>== int)) is false". If this is not meant to work, this is a bug in the sense
> that the error message makes no sense; if this is meant to work, it's a bug in the sense that it doesn't work.
>
> In the former case, feel free to change the priority, severity, and keywords to match: I'm currently assuming that this should work.
>
> --
> typedef int x;
>
> static assert (is (typeof(typeid(x).base) == int));

typeid(x).base is an TypeInfo_Typedef object downcasted to TypeInfo.

Checking the base at runtime:
#
# assert( is(x == typedef)
#	&& (cast(TypeInfo_Typedef)typeid(x)).base.toString() == "int);
#

The compile time check is broken:
#
# // should only succed for int and uint as base types
# static assert(is(x : int) && (! is(x : short)));
#

Background:
#
# static assert(is(int : short));
#
Succeeds, but should fail if D were a strict language. Sadly the "Usual
Arithmetic Conversions" section from http://digitalmars.com/d/type.html
only talks about operand and not about types :(

Thomas

January 01, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=196


thomas-dloop@kuehne.cn changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|rejects-valid               |
         OS/Version|Windows                     |All




------- Comment #1 from thomas-dloop@kuehne.cn  2007-01-01 04:25 -------
The error message is incorrect, the assert should fail regardless.

#
# static assert (is (typeof(typeid(x).base) == int));
#

x -> (typedef int)
typeid(x) -> TypeInfo_Typedef(x)
typeid(x).base -> TypeInfo(int)
typeof(typeid(x).base) -> TypeInfo

Thus rewriting the above assert:
#
# static assert(is(TypeInfo == int));
#

To ensuring that a typedef's direct base type is an int:
#
# typedef byte X;
#
# static if(is(X == typedef) && is(X base == typedef)){
#    static assert(is(typeof(base) == int));
# }
#


-- 

June 23, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=196


bugzilla@digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID




------- Comment #2 from bugzilla@digitalmars.com  2008-06-23 16:36 -------
Thomas' code is almost correct. The correct code would be:

typedef int X;

static if (is(X base == typedef))
{
    static assert(is(base == int), "base of typedef X is not int");
}
else
{
    static assert(0, "X is not a typedef");
}


--