D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 17826 - Inherited functions attribute overridden
Summary: Inherited functions attribute overridden
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Windows
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-09-13 18:16 UTC by John Hall
Modified: 2024-12-13 18:54 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 John Hall 2017-09-13 18:16:45 UTC
When overriding a function, the derived class's version takes on the function attributes of the base class member function. This overrules any type attributes the user will include. In the example below, Bar.foo is @safe @nogc pure nothrow and Foo.bar overrides it with @system, but this is ignored and it is actually @safe @nogc pure nothrow, like Bar.bar. 

This is not an issue of type inference (i.e. that Foo.bar does not have any non-safe commands so the compiler infers it is @safe). If the @system is removed from Foo.bar and some kind of non-safe operations are included in its body, then the compiler has an error that Foo.bar is safe.

I would consider a satisfactory resolution of this issue either or both:
1) Some kind of error message that anything overriding Bar.bar must have the same function attributes
2) The fix for issue 7534 (and associated bugs) only applies to const/immutable/etc member functions. It could be extended to other function attributes. This way, one could write Foo.bar as @system without the override.
https://issues.dlang.org/show_bug.cgi?id=7534



class Bar
{
    void bar(string s) @safe @nogc pure nothrow { }
}

class Foo : Bar
{
    override void bar(string s) @system { } //function is actually @safe @nogc pure nothrow
}

void main()
{
    import std.traits : hasFunctionAttributes;
    
    alias S = typeof(Foo.bar);
    alias T = typeof(Bar.bar);

    static assert(hasFunctionAttributes!(S, "@safe"));
    static assert(hasFunctionAttributes!(T, "@safe"));
    static assert(hasFunctionAttributes!(S, "@nogc"));
    static assert(hasFunctionAttributes!(T, "@nogc"));
    static assert(hasFunctionAttributes!(S, "pure"));
    static assert(hasFunctionAttributes!(T, "pure"));
    static assert(hasFunctionAttributes!(S, "nothrow"));
    static assert(hasFunctionAttributes!(T, "nothrow"));
    
    static assert(!hasFunctionAttributes!(S, "@system"));
    static assert(!hasFunctionAttributes!(T, "@system"));
}
Comment 1 dlangBugzillaToGithub 2024-12-13 18:54:30 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/19316

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB