Bug 21807 – Non-immutable data can be converted to immutable using function call in ctor
Status
RESOLVED
Resolution
INVALID
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-04-07T01:22:10Z
Last change time
2022-08-09T06:51:17Z
Keywords
pull
Assigned to
No Owner
Creator
Temtaime
Comments
Comment #0 by temtaime — 2021-04-07T01:22:10Z
class Foo
{
this()
{
// char[12] pp; str = pp; // error
str = bb(); // no error ???
}
string str;
}
char[12] bb()
{
char[12] res;
return res;
}
Also why it doesn't produce
Error: escaping reference to stack allocated value returned by bb()
???
Comment #1 by temtaime — 2021-04-07T01:28:26Z
Or at least
Deprecation: slice of static array temporary returned by X assigned to longer lived variable Y
Comment #2 by snarwin+bugzilla — 2021-04-07T01:59:11Z
If `bb` and the constructor are marked as @safe, DMD produces the following error:
---
Error: reference to stack allocated value returned by bb() assigned to non-scope this.str
---
Separately from this, compiling with `-preview=dip1000` causes DMD to produce the following error:
---
Error: cannot implicitly convert expression bb() of type char[12] to string
---
Comment #3 by moonlightsentinel — 2021-04-07T03:19:07Z
Up to 2.062 : Failure with output: onlineapp.d(6): Error: cannot implicitly convert expression (bb()) of type char[12LU] to string
Since 2.063 : Success and no output
Comment #4 by moonlightsentinel — 2021-04-07T03:46:27Z
char[] a(const int[] arr) pure @safe;
void b(const int[] arr) @safe
{
char[] c=a(arr);
string d=a(arr);
}
For this code dip1000 rejects the conversion to string too. How is it related to dip1000?
Comment #6 by snarwin+bugzilla — 2021-04-07T19:31:14Z
(In reply to anonymous4 from comment #5)
> char[] a(const int[] arr) pure @safe;
> void b(const int[] arr) @safe
> {
> char[] c=a(arr);
> string d=a(arr);
> }
>
> For this code dip1000 rejects the conversion to string too. How is it
> related to dip1000?
It's not related to dip1000. Passing `-preview=dip1000` causes a different code path to be taken in the compiler, which ends up avoiding this bug by coincidence.
Comment #7 by dlang-bot — 2021-04-07T23:47:40Z
dlang/dmd pull request #12398 "Issue 21807 - Missing deprecation when slicing static array temporary..." was merged into stable:
- 5110b4e6dc9849001b273bd443a2b67ac490dd1b by MoonlightSentinel:
Issue 21807 - Missing deprecation when slicing static array temporary...
...in class methods.
The checks for such slices relied on `va` while actually just caring
about the current type. This was broken by [1] which clears `va` for
class members to detect escapes of delegates.
This also applied to the checks for struct temporaries/literals.
[1] https://github.com/dlang/dmd/pull/8008https://github.com/dlang/dmd/pull/12398
Comment #8 by dlang-bot — 2021-04-08T22:54:19Z
@MoonlightSentinel updated dlang/dmd pull request #12402 "Fix 21229 - Accept construction of union member as initialization" mentioning this issue:
- Issue 21807 - Missing deprecation when slicing static array temporary...
...in class methods.
The checks for such slices relied on `va` while actually just caring
about the current type. This was broken by [1] which clears `va` for
class members to detect escapes of delegates.
This also applied to the checks for struct temporaries/literals.
[1] https://github.com/dlang/dmd/pull/8008https://github.com/dlang/dmd/pull/12402
Comment #9 by dlang-bot — 2021-04-09T08:07:30Z
@MoonlightSentinel created dlang/dmd pull request #12408 "Merge stable into master" mentioning this issue:
- Issue 21807 - Missing deprecation when slicing static array temporary...
...in class methods.
The checks for such slices relied on `va` while actually just caring
about the current type. This was broken by [1] which clears `va` for
class members to detect escapes of delegates.
This also applied to the checks for struct temporaries/literals.
[1] https://github.com/dlang/dmd/pull/8008https://github.com/dlang/dmd/pull/12408
Comment #10 by dlang-bot — 2021-04-09T10:49:02Z
dlang/dmd pull request #12408 "Merge stable into master" was merged into master:
- aea9ec8b1ce67f340a7a95564d3b2e8ae2de8d15 by MoonlightSentinel:
Issue 21807 - Missing deprecation when slicing static array temporary...
...in class methods.
The checks for such slices relied on `va` while actually just caring
about the current type. This was broken by [1] which clears `va` for
class members to detect escapes of delegates.
This also applied to the checks for struct temporaries/literals.
[1] https://github.com/dlang/dmd/pull/8008https://github.com/dlang/dmd/pull/12408
Comment #11 by dlang-bot — 2021-10-20T10:29:02Z
@RazvanN7 created dlang/dmd pull request #13206 "Fix Issue 21807 - Issue 21807 - Non-immutable data can be converted to immutable using function call in ctor" fixing this issue:
- Fix Issue 21807 - Issue 21807 - Non-immutable data can be converted to immutable using function call in ctor
https://github.com/dlang/dmd/pull/13206
Comment #12 by bugzilla — 2022-08-09T06:51:17Z
With current master, and @safe applied, the following deprecation is printed with or without -preview=dip1000:
test.d(8): Deprecation: slice of static array temporary returned by `bb()` assigned to longer lived variable `this.str`
This message is correct.
Note that since the char[12] is a temporary value, there are no other references to it, so it can safely be converted to immutable.
Closing as the compiler is working as expected.