D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 21231 - Unreachable warning for empty struct in VariantN with preview=fieldwise
Summary: Unreachable warning for empty struct in VariantN with preview=fieldwise
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2020-09-08 18:41 UTC by moonlightsentinel
Modified: 2020-10-21 04:52 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description moonlightsentinel 2020-09-08 18:41:25 UTC
The following code produces an "unreachable code" warning when compiled with -preview=fieldwise:

================================================
struct Empty
{
    bool opCmp(const scope ref Empty) const
    { return false; }
}

void main()
{
    import std.variant : VariantN;
    
    Empty a, b;
    assert(a == b);
    assert(!(a < b));

    VariantN!(4, Empty) v = a;
    assert(v == b);
    assert(!(v < b));
}
================================================

/dlang/dmd/linux/bin64/../../src/phobos/std/variant.d(261): Warning: statement is not reachable

================================================

The unreachable code follows an equality check for Empty which always returns true, causing an early exit in VariantN.handler.compare. This skips the checks for < and >.
Comment 1 Dlang Bot 2020-09-08 19:01:06 UTC
@MoonlightSentinel created dlang/phobos pull request #7622 "Enable preview=fieldwise & fix blocking bug in VariantN" fixing this issue:

- Fix 21231 - Unreachable warning for empty struct in VariantN...
  
  ... with preview=fieldwise.
  
  Equality checks will never fail for instances of empty structs, hence
  don't generate code checking `<` and `>` for them.
  
  This was exposed by `-preview=fieldwise` which (as a side effect)
  enables dmd to const-fold the aformentioned equality test in the
  frontend.

https://github.com/dlang/phobos/pull/7622
Comment 2 Dlang Bot 2020-09-10 15:03:31 UTC
@nordlow created dlang/phobos pull request #7624 "Fieldwise revived" fixing this issue:

- Fix 21231 - Unreachable warning for empty struct in VariantN...
  
  ... with preview=fieldwise.
  
  Equality checks will never fail for instances of empty structs, hence
  don't generate code checking `<` and `>` for them.
  
  This was exposed by `-preview=fieldwise` which (as a side effect)
  enables dmd to const-fold the aformentioned equality test in the
  frontend.

https://github.com/dlang/phobos/pull/7624
Comment 3 Dlang Bot 2020-10-21 04:52:58 UTC
dlang/phobos pull request #7624 "Make Phobos compilable with preview=fieldwise" was merged into master:

- c7e49f5afd20e874e2e046d6402b40e5698a282e by MoonlightSentinel:
  Fix 21231 - Unreachable warning for empty struct in VariantN...
  
  ... with preview=fieldwise.
  
  Equality checks will never fail for instances of empty structs, hence
  don't generate code checking `<` and `>` for them.
  
  This was exposed by `-preview=fieldwise` which (as a side effect)
  enables dmd to const-fold the aformentioned equality test in the
  frontend.

https://github.com/dlang/phobos/pull/7624