Skip to content

Comments

fix Issue 18143 - in/out contracts should be implicitly 'this' const#7553

Merged
dlang-bot merged 1 commit intodlang:masterfrom
ibuclaw:issue18143
Jan 6, 2018
Merged

fix Issue 18143 - in/out contracts should be implicitly 'this' const#7553
dlang-bot merged 1 commit intodlang:masterfrom
ibuclaw:issue18143

Conversation

@ibuclaw
Copy link
Member

@ibuclaw ibuclaw commented Dec 30, 2017

As noted by @quickfur.

Actual error message should probably be ratified. I just borrowed the same format as the error above for modifying other parameters in contracts.

@dlang-bot
Copy link
Contributor

Thanks for your pull request, @ibuclaw!

Bugzilla references

Auto-close Bugzilla Description
18143 in/out contracts should be implicitly 'this' const

sc2.flags = (sc2.flags & ~SCOPEcontract) | SCOPErequire;

// BUG: need to error if accessing out parameters
// BUG: need to treat parameters as const
Copy link
Member

Choose a reason for hiding this comment

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

Hmm. This PR seems to only disable writing to this, but does it also prohibit modifying function parameters?

Choose a reason for hiding this comment

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

@quickfur that was implemented in #1569. Look at the whole of checkModify.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, ok. Nevermind then. :-) I was just confused because deleting this comment in this PR makes it seem like this PR is fixing the entire issue. I suppose making this const is the last remaining issue as far as this comment is concerned?

@quickfur
Copy link
Member

quickfur commented Jan 2, 2018

LGTM.

@JinShil
Copy link
Contributor

JinShil commented Jan 2, 2018

Looks like this may have found a bug in Sociomantic's Ocean

./src/ocean/net/http/HttpResponse.d(466): Error: variable ocean.net.http.HttpResponse.HttpResponse.AppendHeaderLines.IncrementalValue.this cannot modify parameter 'this' in contract

cc @leandro-lucarella-sociomantic @mathias-lang-sociomantic

@quickfur
Copy link
Member

quickfur commented Jan 2, 2018

Very nice! I love compiler changes that discover bugs in existing code!

@ibuclaw
Copy link
Member Author

ibuclaw commented Jan 3, 2018

mihails-strasuns-sociomantic added a commit to mihails-strasuns-sociomantic/ci that referenced this pull request Jan 5, 2018
Contains fix for the bug which will be detected by
dlang/dmd#7553
@wilzbach
Copy link
Contributor

wilzbach commented Jan 5, 2018

Looks like this may have found a bug in Sociomantic's Ocean

Thanks to the work of @iain-buclaw-sociomantic and @mihails-strasuns-sociomantic Ocean was fixed and upgraded:

sociomantic-tsunami/ocean#427
dlang/ci#108

-> Jenkins should be passing now :)

Copy link
Contributor

@wilzbach wilzbach left a comment

Choose a reason for hiding this comment

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

We should really stop documenting bugs in the source code where they are just forgotten or confuse people because it has been fixed. Should we add the fixed "BUGS" as test cases to the testsuite?

sc2.flags = (sc2.flags & ~SCOPEcontract) | SCOPEensure;

// BUG: need to treat parameters as const
// BUG: need to disallow returns and throws
Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't document bugs in the source code like this, they should be in Bugzilla:

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Also return is already forbidden: https://run.dlang.io/is/A0JWgT

Choose a reason for hiding this comment

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

I didn't verify any of the comments, so no idea what the state is.

@@ -875,7 +875,6 @@ extern(C++) final class Semantic3Visitor : Visitor
sc2.flags = (sc2.flags & ~SCOPEcontract) | SCOPErequire;

// BUG: need to error if accessing out parameters
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this already forbidden?

https://run.dlang.io/is/NacqYo

Choose a reason for hiding this comment

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

But is that accessing the out parameter, or out parameters?

Copy link
Member Author

@ibuclaw ibuclaw Jan 7, 2018

Choose a reason for hiding this comment

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

Actually, I've worked out what this means:

static int cache;

int foo(out int bar)
in
{
    cache = bar;       // This should be an error.
    somefunc(bar);     // This should be an error.
    assert(bar == 0);  // This should be an error.
}

Copy link
Member Author

Choose a reason for hiding this comment

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

@wilzbach
Copy link
Contributor

wilzbach commented Jan 6, 2018

(rebased to hopefully fix Jenkins)

@dlang-bot dlang-bot merged commit 4c7ea17 into dlang:master Jan 6, 2018
@quickfur
Copy link
Member

quickfur commented Jan 6, 2018

Excellent!

// BUG: need to error if accessing out parameters
// BUG: need to treat parameters as const
// BUG: need to disallow returns and throws
// BUG: verify that all in and ref parameters are read
Copy link
Member Author

Choose a reason for hiding this comment

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

This comment as seen today appeared in D1.011.

Before it used to be:

BUG: verify that all in and inout parameters are read

Which has been here since the dawn of time svn history.

Copy link
Member Author

@ibuclaw ibuclaw Jan 7, 2018

Choose a reason for hiding this comment

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

It seems to allude to this:

int foo(in int a, ref int b)
in
{
    // Should emit error: 'a' is not read in contract.
    // Should emit error: 'b' is not read in contract.
}
do
{
    return a + b;
}

But I don't think that any of those are a compiler bug at all, and so this comment should be removed. @WalterBright do I understand the context correctly?

Copy link
Member

Choose a reason for hiding this comment

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

Why should an error be emitted if a parameter isn't read in the in-contract? How else would you express that any possible value is permitted for that parameter?

Copy link
Contributor

Choose a reason for hiding this comment

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

Some languages enforce that some parameter are used, e.g. C# forces you to assign out parameters. One way to express this in an in contract would be to do cast(void)a; for example. But that's the kind of enforcement which IMO goes against D's approach, as many times you'll end up just trying to satisfy the compiler.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm saying I don't think it should be considered as a bug. Also the comments doesn't say anything about all parameters. Just in and ref parameters.

Copy link
Contributor

Choose a reason for hiding this comment

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

verify that all in and ref parameters are read

It may also be implying that all input parameters should actually be used in the implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants