D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20917 - stacking alias this, drops data during assignment
Summary: stacking alias this, drops data during assignment
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P1 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-06-09 21:35 UTC by crazymonkyyy
Modified: 2020-06-09 23:47 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description crazymonkyyy 2020-06-09 21:35:15 UTC
struct ihatecompilers(T,size_t n){
  struct hate{
    T payload; alias payload this;
    float depthofhatred=float.max;
  }
  hate data;
  void opAssign(T a){
    data=a;
    import std.stdio;
    data.writeln;
    assert(data==a);
  }
}
unittest{
  struct inner{int a;}
  struct outer{
    inner payload;alias payload this;
    string metadata;
  }
  ihatecompilers!(outer,100) foo;
  inner bar= inner(10);
  foo=outer(bar,"very very very important data");
}

foo is assigned as outer(inner(10),""), droping the very very important data
Comment 1 Stanislav Blinov 2020-06-09 22:53:03 UTC
    void opAssign(T a) {
        data = a;
        // hate = outer; Needs implicit conversion.
        // outer converts to inner.
        // hate = inner; Needs implicit conversion.
        // No implicit conversion found. Lookup through hate's alias this.
        // getAliasThis(hate) -> outer.
        // outer = inner; Needs implicit conversion.
        // No implicit conversion found. Lookup through outer's alias this.
        // getAliasThis(outer) -> inner.
        // inner = inner;
        assert(data == a); // makes incorrect assumption
    }

You need to define opAssign(T) for hate.