D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5153 - Struct pointer to struct variable assign error message
Summary: Struct pointer to struct variable assign error message
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 minor
Assignee: No Owner
URL:
Keywords: bootcamp, diagnostic
Depends on:
Blocks:
 
Reported: 2010-11-01 13:24 UTC by bearophile_hugs
Modified: 2018-05-18 07:50 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2010-11-01 13:24:03 UTC
This is wrong D2 code, it contains a common mistake (f is a Foo instead of the correct Foo*):


struct Foo {
    int x;
    this(int x_) {
        this.x = x_;
    }
}
void main() {
    Foo f = new Foo(0);
}


DMD 2.050 gives the error messages that don't look correct:

test.d(8): Error: constructor test3.Foo.this (int x_) is not callable using argument types (Foo*)
test.d(8): Error: cannot implicitly convert expression (new Foo(0)) of type Foo* to int


But I was expecting a single error message similar to:

test.d(8): Error: cannot implicitly convert expression (new Foo(0)) of type Foo* to Foo
Comment 1 Andrej Mitrovic 2014-04-28 12:01:08 UTC
It's not a wrong diagnostic, but maybe it can be improved somehow. The issue is that after construction the compiler will attempt another construction. E.g.:

-----
struct Foo
{
    this(Foo* x_) { }
    this(int x_) { }
}

void main()
{
    // calls two ctors
    Foo f = new Foo(0);
}
-----

Maybe this can be considered dangerous behavior. I'll CC Kenji for thoughts.
Comment 2 RazvanN 2018-04-23 12:20:26 UTC
PR : https://github.com/dlang/dmd/pull/8203/files
Comment 3 github-bugzilla 2018-05-18 07:50:53 UTC
Commits pushed to master at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/14dfe15bf5aad2c6638f1552b6250416b6b7c2b4
Fix Issue 5153 - Struct pointer to struct variable assign error message

https://github.com/dlang/dmd/commit/b61d1c8989bed54489c9c7eb5acc2e1f4312b8c6
Merge pull request #8203 from RazvanN7/Issue_5153

Fix Issue 5153 - Struct pointer to struct variable assign error message
merged-on-behalf-of: Mike Franklin <JinShil@users.noreply.github.com>