D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20724 - Compiler fails to infer @safe
Summary: Compiler fails to infer @safe
Status: RESOLVED DUPLICATE of issue 7205
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-04-07 07:34 UTC by Andrej Mitrovic
Modified: 2020-06-05 16:01 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 Andrej Mitrovic 2020-04-07 07:34:37 UTC
-----
public void hash (T) (scope const auto ref T record) // @safe  // uncomment to compile
    if (is(T == struct))
{
    foreach (const ref field; record.tupleof)
    {
        hash(field);
    }
}

public void hash (T) (scope const auto ref T[] records) @safe
{
    foreach (ref record; records)
        hash(record);
}

@safe void main ()
{
    struct Foo
    {
        Foo[] foo;
    }

    Foo foo;
    hash(foo);
}
-----

$ dmd test.d
$ test.d(15): Error: @safe function test.hash!(Foo).hash cannot call @system function test.hash!(Foo).hash
$ test.d(3):        test.hash!(Foo).hash is declared here

Uncommenting '@safe' makes it compile. The safety should have been inferred.
Comment 1 Paul Backus 2020-06-05 16:01:51 UTC

*** This issue has been marked as a duplicate of issue 7205 ***