D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6931 - scope parameter storage class not checked at all
Summary: scope parameter storage class not checked at all
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid, diagnostic
: 15562 18129 (view as issue list)
Depends on:
Blocks:
 
Reported: 2011-11-11 07:17 UTC by Trass3r
Modified: 2020-08-04 03:38 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 Trass3r 2011-11-11 07:17:58 UTC
int ga;
C gb;
S* gc;
int[] gd;

struct S {}
class C {}

void foo(scope int a, scope C b, scope S* c, scope int[] d)
{
	ga = a;
	gb = b;
	gc = c;
	gd = d;
	ga = d[1];
}

void main()
{
	S s;
	foo(5, new C, &s, [1,2,3]);
}

This compiles and runs fine. Shouldn't at least some of these cause errors?
Comment 1 Denis Shelomovskii 2012-01-22 06:47:29 UTC
If we will look at destruction & allocation:
---
import std.stdio;

class C {
    int n;
    this(int n) { writefln(" this(%s) at %s", this.n = n, cast(void*)this); }
    ~this() { writefln("~this(%s) at %s", n, cast(void*)this); }
}

void main() {
    int i;
    writefln("Stack is at %s", &i);
    writefln("Heap  is at %s", (new void[1]).ptr);
    {
        C cHeap = new C(0); // will be destroyed on scope exit
        scope C c0 = cHeap;

        // C(1)-C(4) will be allocated in heap
        // C(1), C(2), and C(4) will be destroyed on scope exit
        // C(3) will be destroyed on garbage collection
        scope C c1 = cast(C)cast(void*)new C(1);
        scope C c2 = true ? new C(2) : null;
        scope C c3 = (new C(3), new C(4));
    }
    writefln("after scope");
}
---
As a result even if `C` is a `scope class` the program will compile without `cHeap` and `c0`, but every `C` instance will be allocated in heap and C(3) will be destroyed on garbage collection.
Comment 2 Trass3r 2012-01-22 07:02:54 UTC
This report is about scope as a parameter storage class!
Comment 3 Denis Shelomovskii 2012-01-22 09:19:58 UTC
Oh, sorry for the unrelated comment. Created Issue 7347 for that.
Comment 4 basile-z 2016-01-15 04:14:22 UTC
*** Issue 15562 has been marked as a duplicate of this issue. ***
Comment 5 Jonathan M Davis 2018-03-16 19:50:17 UTC
*** Issue 18129 has been marked as a duplicate of this issue. ***
Comment 6 Mathias LANG 2020-08-04 03:38:16 UTC
Solved by DIP1000.