View mode: basic / threaded / horizontal-split · Log in · Help
July 30, 2011
[Issue 6408] New: string[].init gives a wrong type
http://d.puremagic.com/issues/show_bug.cgi?id=6408

          Summary: string[].init gives a wrong type
          Product: D
          Version: D2
         Platform: x86
       OS/Version: Windows
           Status: NEW
         Keywords: wrong-code
         Severity: normal
         Priority: P2
        Component: DMD
       AssignedTo: nobody@puremagic.com
       ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2011-07-30 06:56:07 PDT ---
I think this D2 code shows an error:

import std.stdio;
void main() {
   writeln(typeid(typeof(string[].init)));
   writeln(typeid(typeof(string[][].init)));
   writeln(typeid(typeof(string[][][].init)));
   writeln(typeid(typeof((string[]).init)));
}


Output, DMD 2.054:
immutable(char)[]
immutable(char)[]
immutable(char)[]
immutable(char)[][]

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 30, 2011
[Issue 6408] string[].init gives a wrong type
http://d.puremagic.com/issues/show_bug.cgi?id=6408


kennytm@gmail.com changed:

          What    |Removed                     |Added
----------------------------------------------------------------------------
          Keywords|wrong-code                  |accepts-invalid
                CC|                            |kennytm@gmail.com


--- Comment #1 from kennytm@gmail.com 2011-07-30 08:44:22 PDT ---
Apparently DMD shouldn't accept string[].init at all, e.g. int[].init is a
parser error:

--------------------------
alias int[] F;
//enum f = int[].init;  // error (as expected)
enum g = F[].init;      // no error, return 'null' of type F.
--------------------------

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 30, 2011
[Issue 6408] string[].init gives a wrong type
http://d.puremagic.com/issues/show_bug.cgi?id=6408



--- Comment #2 from bearophile_hugs@eml.cc 2011-07-30 09:13:40 PDT ---
(In reply to comment #1)
> Apparently DMD shouldn't accept string[].init at all, e.g. int[].init is a
> parser error:

Isn't it better to modify DMD to accept both string[].init and int[].init, and
return the correct results in both cases?

Because the alternative idiom to create an empty array is to use cast(int[])[],
and it's better to avoid casts where possible.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 30, 2011
[Issue 6408] string[].init gives a wrong type
http://d.puremagic.com/issues/show_bug.cgi?id=6408



--- Comment #3 from kennytm@gmail.com 2011-07-30 12:03:14 PDT ---
(In reply to comment #2)
> (In reply to comment #1)
> > Apparently DMD shouldn't accept string[].init at all, e.g. int[].init is a
> > parser error:
> 
> Isn't it better to modify DMD to accept both string[].init and int[].init, and
> return the correct results in both cases?
> 
> Because the alternative idiom to create an empty array is to use cast(int[])[],
> and it's better to avoid casts where possible.

You could also just add a pair of parenthesis:

   (string[]).init
   (int[]).init

And (X[]).init returns 'null', not '[]'.


I'm not sure about allowing `S[].prop`. If this is allowed, we should also
allow `S[3].prop` and `S[T].prop` and maybe even `S*.prop`. Maybe let's have a
rejects-valid or enhancement request.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 30, 2011
[Issue 6408] string[].init gives a wrong type
http://d.puremagic.com/issues/show_bug.cgi?id=6408



--- Comment #4 from bearophile_hugs@eml.cc 2011-07-30 12:37:31 PDT ---
(In reply to comment #3)

> You could also just add a pair of parenthesis:
> 
>     (string[]).init
>     (int[]).init

This was my last example.


> I'm not sure about allowing `S[].prop`. If this is allowed, we should also
> allow `S[3].prop` and `S[T].prop` and maybe even `S*.prop`. Maybe let's have a
> rejects-valid or enhancement request.

Beside returning the correctly typed value, as alternative I accept this:

string[].init

to produce a syntax error that suggests the programmer to use (string[]).init
instead. What I don't accept it silently returning a value of the "wrong" type.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 30, 2011
[Issue 6408] string[].init gives a wrong type
http://d.puremagic.com/issues/show_bug.cgi?id=6408



--- Comment #5 from kennytm@gmail.com 2011-07-30 13:41:25 PDT ---
(In reply to comment #4)
> (In reply to comment #3)
> 
> > You could also just add a pair of parenthesis:
> > 
> >     (string[]).init
> >     (int[]).init
> 
> This was my last example.
> 

Right.

> > I'm not sure about allowing `S[].prop`. If this is allowed, we should also
> > allow `S[3].prop` and `S[T].prop` and maybe even `S*.prop`. Maybe let's have a
> > rejects-valid or enhancement request.
> 
> Beside returning the correctly typed value, as alternative I accept this:
> 
> string[].init
> 
> to produce a syntax error that suggests the programmer to use (string[]).init
> instead. What I don't accept it silently returning a value of the "wrong" type.

I agree.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 16, 2013
[Issue 6408] string[].init gives a wrong type
http://d.puremagic.com/issues/show_bug.cgi?id=6408


yebblies <yebblies@gmail.com> changed:

          What    |Removed                     |Added
----------------------------------------------------------------------------
          Keywords|                            |pull
                CC|                            |yebblies@gmail.com
          Platform|x86                         |All
        AssignedTo|nobody@puremagic.com        |yebblies@gmail.com
        OS/Version|Windows                     |All


--- Comment #6 from yebblies <yebblies@gmail.com> 2013-01-17 01:00:26 EST ---
T[], T[T] and T[N]

I'm not sure T* can be done like this, but it also produces an error instead of
being ignored silently.

https://github.com/D-Programming-Language/dmd/pull/1495

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 16, 2013
[Issue 6408] string[].init gives a wrong type
http://d.puremagic.com/issues/show_bug.cgi?id=6408



--- Comment #7 from bearophile_hugs@eml.cc 2013-01-16 09:58:21 PST ---
(In reply to comment #6)

> I'm not sure T* can be done like this, but it also produces an error instead of
> being ignored silently.

Thank you.
In the unittests have you added a test case that shows such error message?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 17, 2013
[Issue 6408] string[].init gives a wrong type
http://d.puremagic.com/issues/show_bug.cgi?id=6408



--- Comment #8 from yebblies <yebblies@gmail.com> 2013-01-17 11:35:14 EST ---
(In reply to comment #7)
> (In reply to comment #6)
> 
> > I'm not sure T* can be done like this, but it also produces an error instead of
> > being ignored silently.
> 
> Thank you.
> In the unittests have you added a test case that shows such error message?

For:

auto x = string*.init;

You get:

testx.d(3): Error: undefined identifier 'init'

Because it is parsed as (string) * (.init).  To me it is much more clear that
string*.init is invalid code compared to string[].init.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 21, 2013
[Issue 6408] string[].init gives a wrong type
http://d.puremagic.com/issues/show_bug.cgi?id=6408



--- Comment #9 from github-bugzilla@puremagic.com 2013-01-20 23:00:22 PST ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/064b0419f8956a76f3d7cabeacd0861cff802923
Fix Issue 6408 - string[].init gives a wrong type

Allow reinterpreting a slice or index expression as a dynamic array, static
array, or associative array

https://github.com/D-Programming-Language/dmd/commit/cd9ef35c402dbeb177343c7cea5b9ed4a5e6b94f
Merge pull request #1495 from yebblies/issue6408

Fix Issue 6408 - string[].init gives a wrong type

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home