[Issue 3605] Compiler accepts invalid variable declaration, which does not link

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Dec 11 03:20:10 PST 2009


http://d.puremagic.com/issues/show_bug.cgi?id=3605


Lars T. Kyllingstad <bugzilla at kyllingen.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla at kyllingen.net


--- Comment #2 from Lars T. Kyllingstad <bugzilla at kyllingen.net> 2009-12-11 03:20:09 PST ---
This is expected and well-defined behaviour. I'll explain:


(In reply to comment #0)
> 1. struct, class and union type: Error 42: Symbol Undefined
> _D4main4mainFZv1sMFZS4main2S2
> 
> struct S { int x; }
> void main ()
> {

Here you declare a function s() which returns an S, without the function body.
The linker will expect to find it in some library or object file.

>    S s();  // braces should not be allowed.
>    s.x = 1; // comment this line and example compiles and runs
> }
> 
> 
> 2. int type: Error 42: Symbol Undefined _D4main4mainFZv1iMFZi
> 
> void main ()
> {
>     int i(); // braces should not be allowed.

Same here, the compiler assumes that the function i() will be linked in later,
and therefore accepts it. The next line works because of the property syntax
for calling functions. Writing i.max is equivalent to writing i().max. The
property max is available for all ints.

>     int a = i.max; // surprisingly this and next line works.
>     writeln (a);

The next line also works because of the property syntax. Writing i=1 is
equivalent to writing i(1).

>     //i = 1; // uncomment this to cause error
>     //writeln (i); // or this
> }
> 
> 
> 2b. this example works correctly – compiler gives some error message, even if
> not so appropriate.
> Error: function main.main.i () is not callable using argument types (int)
> Error: expected 0 arguments, not 1 for non-variadic function type int()

This is because the compiler interprets i=1 as i(1), which doesn't work since
you have declared i() without any parameters.

> void main ()
> {
>     int i();
>     i = 1;
> }


Note that this confusing stuff will most likely disappear in the near future,
as property functions will have to be annotated with the @property attribute.

  void foo(int i);
  foo = 123;           // won't work, must write foo(123)

  @property void bar(int i);
  bar = 123;           // that's more like it

You can read about it in the spec, but note again that @property isn't yet
REQUIRED for this to work:

  http://www.digitalmars.com/d/2.0/function.html#property-functions

-Lars

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list