D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7616 - aggregates don't inherit pure, @nogc, nothrow from outer scope
Summary: aggregates don't inherit pure, @nogc, nothrow from outer scope
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2012-02-29 23:02 UTC by Martin Nowak
Modified: 2024-12-13 17:58 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Martin Nowak 2012-02-29 23:02:12 UTC
@safe:             // works
pure nothrow:      // doesn't work

struct Foo1
{
    void bar() {}
}

struct Foo2
{
pure nothrow:      // redundant
    void bar() {}
}

pragma(msg, typeof(Foo1.bar));
pragma(msg, typeof(Foo2.bar));

@property doesn't work either.
--------

I liked Don's idea of an attribute stack in the parser
to get rid of these inconsistencies.
Comment 1 SomeDude 2012-04-20 02:46:05 UTC
What do you mean by "doesn't work ?"
This code compiles on 2.059, but I'm not sure what you wanted to test.
Comment 2 hsteoh 2014-08-18 20:02:04 UTC
Gah, this is a mess. On git HEAD, the original produces this output:
------
@safe void()
pure nothrow @safe void()
------

which seems to indicate that "pure nothrow:" has no effect.

Commenting out the "pure nothrow:" line in Foo2 produces:
------
@safe void()
@safe void()
------

So basically, "pure nothrow:" doesn't work at all.
Comment 3 Neia Neutuladh 2018-12-13 21:43:09 UTC
https://github.com/dlang/dmd/pull/9076
Comment 4 Neia Neutuladh 2018-12-14 20:11:50 UTC
The spec at https://dlang.org/spec/attribute.html says:

attribute:     // affects all declarations until the end of
               // the current scope

The obvious interpretation is that any declaration between `attribute:` and the end of the current scope should have `attribute` applied. This includes nested declarations.

A version that doesn't include nested declarations would say something like "affects all declarations within the current scope after `attribute:`", ideally with clarification about nested scopes.

If there were any such text, then @safe would need to *not* propagate for consistency, *or* there would need to be other text saying that @safe propagates differently (which currently there isn't).

More illustrative test case:

---
nothrow pure
{
struct Foo
{
  int b() { return 1; }
}
}
int k() nothrow pure
{
  Foo foo;
  return foo.b();
}
---

Expected result: k() returns 1.
Actual result:
scratch.d(11): Error: pure function scratch.k cannot call impure function scratch.Foo.b
scratch.d(11): Error: function scratch.Foo.b is not nothrow
scratch.d(8): Error: nothrow function scratch.k may throw
Comment 5 Dlang Bot 2024-08-03 13:53:37 UTC
@ntrel created dlang/dlang.org pull request #3893 "[spec] Improve attribute propagation docs" mentioning this issue:

- [spec] Improve attribute propagation docs
  
  Part of Bugzilla 7616 - aggregates don't inherit pure nothrow from outer scope
  
  Add grammar heading.
  `@nogc`, `nothrow` and `pure` do not propagate inside aggregates.
  Add example.
  Add link to core.attribute.

https://github.com/dlang/dlang.org/pull/3893
Comment 6 Dlang Bot 2024-08-03 15:39:54 UTC
dlang/dlang.org pull request #3893 "[spec] Improve attribute propagation docs" was merged into master:

- d55c919d5de5e34235c24f40eb734d54d40f83e1 by Nick Treleaven:
  [spec] Improve attribute propagation docs
  
  Part of Bugzilla 7616 - aggregates don't inherit pure nothrow from outer scope
  
  Add grammar heading.
  `@nogc`, `nothrow` and `pure` do not propagate inside aggregates.
  Add example.
  Add link to core.attribute.

https://github.com/dlang/dlang.org/pull/3893
Comment 7 dlangBugzillaToGithub 2024-12-13 17:58:45 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

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

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