Comment #0 by john.michael.hall — 2017-09-13T18:16:45Z
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 by robert.schadek — 2024-12-13T18:54:30Z