Skip to content

fix Issue 16747 - Cannot have stack allocated classes in @safe code#6279

Merged
MartinNowak merged 1 commit intodlang:stablefrom
WalterBright:fix16747
Dec 15, 2016
Merged

fix Issue 16747 - Cannot have stack allocated classes in @safe code#6279
MartinNowak merged 1 commit intodlang:stablefrom
WalterBright:fix16747

Conversation

@WalterBright
Copy link
Member

Stack allocated classes with no destructors do not need to call delete.

@dlang-bot
Copy link
Contributor

dlang-bot commented Nov 24, 2016

Fix Bugzilla Description
16747 [Reg 2.072] Cannot have stack allocated classes in @safe code

void fwd() @safe
{
scope o = new Object();
scope c = new C();
Copy link
Contributor

Choose a reason for hiding this comment

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

If the dtor is safe, why wouldn't we allow destruction ?

Copy link
Member

Choose a reason for hiding this comment

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

I think it's because delete uses a C interface and destroys using TypeInfo.

Copy link
Contributor

Choose a reason for hiding this comment

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

But surely at this point, we know the destruction is @safe ?

Copy link
Member

@MartinNowak MartinNowak left a comment

Choose a reason for hiding this comment

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

Please add a separate error message (e.g. "deleting classes with destructors is unsafe atm.") for the hasDtor case, it seems like an internal limitation of the dmd/druntime API, and it would be very hard for people to understand why their code breaks by adding a dtor.

@MartinNowak
Copy link
Member

Non-risky bugfixes and in particular all regression fixes should always go into stable.
It's unacceptable that you're constantly ignoring this simple rule.
We've already missed several regression fixes in previous releases due to this not being followed. We could use the dlang-bot to catch this.

I've changed the target branch to stable, please rebase your PR.
See http://stackoverflow.com/a/10853956 for how to do that.

@MartinNowak MartinNowak changed the base branch from master to stable November 26, 2016 07:23
@MartinNowak
Copy link
Member

Ping

@MartinNowak
Copy link
Member

Something along the line of git rebase --onto upstream/stable upstream/master fix16747 should do the trick. Make sure to fetch upstream beforehand.

@mihails-strasuns
Copy link

@MartinNowak FYI: I have rebased it and would highly recommend everyone to take similar approach. Wait time on back-and-forth requests for technical trivialities is quite a productivity killer - it is much more practical to make such adjustments directly and move on.

@WalterBright WalterBright force-pushed the fix16747 branch 3 times, most recently from f44da90 to 43ddb33 Compare November 28, 2016 09:49
* TEST_OUTPUT:
---
fail_compilation/test16195.d(13): Error: delete p is not @safe but is used in @safe function test
fail_compilation/test16195.d(13): Error: cannot use 'delete' in @nogc function 'test16195.test'
Copy link
Contributor

Choose a reason for hiding this comment

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

delete is not @nogc ?

Copy link
Member Author

Choose a reason for hiding this comment

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

new/delete are part of the gc.

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense. I was under the impression that it would affect scope class, but it doesn't.
However, the following now compiles with the P.R. (commit 43ddb33):

@safe void dl (int* v) { delete v; }

Which is not the intent of this P.R.

Copy link
Member Author

Choose a reason for hiding this comment

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

good catch

@WalterBright WalterBright force-pushed the fix16747 branch 2 times, most recently from 5ce65bc to 66d15ca Compare November 28, 2016 12:37
TEST_OUTPUT:
---
fail_compilation/fail14486.d(44): Error: delete c0 is not @safe but is used in @safe function test1a
fail_compilation/fail14486.d(44): Error: cannot use 'delete' in @nogc function 'fail14486.test1a'
Copy link
Contributor

Choose a reason for hiding this comment

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

My comments applied to all the change to the test suite.

void main () @safe
{
    auto f = new Object();
    delete f;
}

Compiles with your P.R., and obviously shouldn't

@mathias-lang-sociomantic
Copy link
Contributor

What about the following ?

void c () @safe
{
    scope o = new Object;
    delete o;
    auto x = o.toHash();
}

I'd suggest we just prohibit explicit destruction of scope instance

@mihails-strasuns
Copy link

This definitely requires adding distinction between compiler-injected delete call and manual one (i.e. define __delete_automatic which does the same as delete but is allowed in @safe.

@WalterBright
Copy link
Member Author

@Dicebot yes, I think you're right. But I don't have time to work on this at the moment, it'll have to wait a bit.

@WalterBright WalterBright force-pushed the fix16747 branch 5 times, most recently from 7439d2b to 4ac3fbe Compare December 2, 2016 10:54
@MartinNowak
Copy link
Member

@MartinNowak FYI: I have rebased it and would highly recommend everyone to take similar approach. Wait time on back-and-forth requests for technical trivialities is quite a productivity killer - it is much more practical to make such adjustments directly and move on.

I explicitly didn't do it, b/c I need Walter to learn how to target the stable branch. It doesn't help if you or I constantly have to rebase PRs from other people. Maybe we should reconsider a few github workflows to make this smoother, but the fact that people already have problems w/ only 2 branches is fairly frustrating.
And the rule is really trivial, if you want to fix sth. that has already been released, you branch from stable and target stable, always.

@WalterBright
Copy link
Member Author

ping @MartinNowak

Copy link
Contributor

@mathias-lang-sociomantic mathias-lang-sociomantic left a comment

Choose a reason for hiding this comment

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

LGTM

src/expression.h Outdated

class DeleteExp : public UnaExp
{
bool isRIAA;

Choose a reason for hiding this comment

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

typo

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

@mihails-strasuns
Copy link

FYI @MartinNowak this is the PR we have mentioned yesterday that most likely needs to be moved to scope branch. Please sanity check me.

@mathias-lang-sociomantic
Copy link
Contributor

Why ? It's a regression in 2.072.0. It is valid code which stopped to compile.

@mihails-strasuns
Copy link

It opens an infinite hole in @safe without lifetime checks like ones implememted in scope branch.

@mathias-lang-sociomantic
Copy link
Contributor

It opens an infinite hole in @safe without lifetime checks like ones implememted in scope branch.

This hole was already there before 2.072.0. What 2.072.0 did was breaking code that could be valid (but could not be verified by the compiler). And breaking valid code is not something we should ever do.

@MartinNowak
Copy link
Member

MartinNowak commented Dec 15, 2016

Yes, but thanks for mentioning this anyhow, it's not obvious from the PR and prolly wasn't considered.

Disallowing the use of DeleteExp in @safe code (#5887) did incidentally fix escaping of references to stack allocated classes, by bluntly breaking all usage of scope classes.
Usage of scope classes in @safe code was a safety hole before 2.072.0 and should be addressed by the scope/DIP1000 work.
While this PR will reopen safety holes w/ scope classes in 2.072.2, it's much more important to not have an intermediate (until scope/DIP1000 work) breakage of all stack allocated classes in @safe code.

@MartinNowak
Copy link
Member

Auto-merge toggled on

@MartinNowak
Copy link
Member

There is one failing test on OSX (even after repeating it).
https://auto-tester.puremagic.com/show-run.ghtml?projectid=14&runid=2312526&isPull=true
https://auto-tester.puremagic.com/show-run.ghtml?projectid=14&runid=2311102&isPull=true
It rather looks like an issue with the OSX auto tester machine (D-Autotester.local (98.229.209.219)) @braddr.
So I'm still merging this because the same OSX tests on Travis-CI do pass and it fixes a major regression.

@MartinNowak MartinNowak merged commit 6936a2e into dlang:stable Dec 15, 2016
MartinNowak added a commit to MartinNowak/dmd that referenced this pull request Dec 19, 2016
fix Issue 16747 - Cannot have stack allocated classes in @safe code
MartinNowak added a commit to MartinNowak/dmd that referenced this pull request Dec 19, 2016
fix Issue 16747 - Cannot have stack allocated classes in @safe code
WalterBright added a commit that referenced this pull request Dec 19, 2016
Merge pull request #6279 from WalterBright/fix16747
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants