Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/dmd/typesem.d
Original file line number Diff line number Diff line change
Expand Up @@ -795,10 +795,13 @@ private extern (C++) final class TypeSemanticVisitor : Visitor
tf.next = tf.next.typeSemantic(loc, sc);
sc = sc.pop();
errors |= tf.checkRetType(loc);
if (tf.next.isscope() && !(sc.flags & SCOPE.ctor))
if (!global.params.vsafe) // https://issues.dlang.org/show_bug.cgi?id=18295
{
mtype.error(loc, "functions cannot return `scope %s`", tf.next.toChars());
errors = true;
if (tf.next.isscope() && !(sc.flags & SCOPE.ctor))
{
mtype.error(loc, "functions cannot return `scope %s`", tf.next.toChars());
errors = true;
}
}
if (tf.next.hasWild())
wildreturn = true;
Expand Down
19 changes: 19 additions & 0 deletions test/compilable/test18295.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// REQUIRED_ARGS: -dip1000

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

// See test/fail_compilation/test18295.d for the non-`-dip1000` version`

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without @safe: there are no scope checks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added @safe attributes

scope class C { int i; } // Notice the use of `scope` here
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scope class is irrelevant for this test, please remove the misleading comment.
Only the fact that c is a scoped class reference matters for the return scope check.

Copy link
Contributor Author

@JinShil JinShil Jan 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not irrelevant, and in fact is the whole point of this PR. Prior to this PR, due to the scope class attribution, an error would be emitted at the function declaration C increment(scope return C c) @safe, which is too conservative. After this PR, there is no error.


C increment(scope return C c) @safe // Prior to the fix for 18295 an error would be emitted here
{ // which is too conservative
c.i++;
return c;
}

void main() @safe
{
scope C c = new C();
c.increment();
}