[Issue 17826] New: Inherited functions attribute overridden

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Wed Sep 13 18:16:45 UTC 2017


https://issues.dlang.org/show_bug.cgi?id=17826

          Issue ID: 17826
           Summary: Inherited functions attribute overridden
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Windows
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: john.michael.hall at gmail.com

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"));
}

--


More information about the Digitalmars-d-bugs mailing list