Skip to content

Fix Issue 19754 - cast() sometimes yields lvalue, sometimes yields rvalue#9505

Merged
dlang-bot merged 1 commit intodlang:masterfrom
RazvanN7:Alt_19754
May 24, 2019
Merged

Fix Issue 19754 - cast() sometimes yields lvalue, sometimes yields rvalue#9505
dlang-bot merged 1 commit intodlang:masterfrom
RazvanN7:Alt_19754

Conversation

@RazvanN7
Copy link
Contributor

@RazvanN7 RazvanN7 commented Mar 28, 2019

The check for modifiable lvalues was done after an optimization pass that did some const folding which was not performed in the case of shared. This led to accepting invalid code in the case of casted shared data and confusing error messages for other qualified data which was cast to unqualified.

Later edit: I added the missing bits so that the code now behaves according to the spec; that is: cast() and cast($qualifier) returns an lvalue.

@dlang-bot
Copy link
Contributor

dlang-bot commented Mar 28, 2019

Thanks for your pull request and interest in making D better, @RazvanN7! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the annotated coverage diff directly on GitHub with CodeCov's browser extension
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Auto-close Bugzilla Severity Description
19754 normal cast() sometimes yields lvalue, sometimes yields rvalue

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub fetch digger
dub run digger -- build "master + dmd#9505"

@RazvanN7 RazvanN7 force-pushed the Alt_19754 branch 2 times, most recently from 231fef0 to 6c2d367 Compare March 29, 2019 10:27
/*
TEST_OUTPUT:
---
fail_compilation/ice14929.d(45): Error: `cast(Node)(*this.current).items[this.index]` is not an lvalue and cannot be modified
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this was casting a const(Node) to a Node.

/*
TEST_OUTPUT:
---
fail_compilation/diag4596.d(15): Error: `this` is not an lvalue and cannot be modified
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The branches of the ?: operator may return different things, e.g. an rvalue and an rvalue; this way the error is printed depending on the branch

override bool isLvalue()
{
//printf("e1.type = %s, to.type = %s\n", e1.type.toChars(), to.toChars());
return e1.type.mutableOf().unSharedOf() == to.mutableOf().unSharedOf();
Copy link
Member

Choose a reason for hiding this comment

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

Tests look good now, but shouldn't this use implicitConvTo to satisfy "cast(U) expressions applied to lvalues of type T when T* is implicitly convertible to U*;"?

@andralex
Copy link
Member

andralex commented Apr 3, 2019

As discussed with @RazvanN7 : the way slices with constant limits are handled is an irregularity in the language. For the time being I suggest we support and document it.

@9il
Copy link
Member

9il commented Apr 13, 2019

I put a link here, hopefully, the pr will fix it https://issues.dlang.org/show_bug.cgi?id=19793

@RazvanN7 RazvanN7 force-pushed the Alt_19754 branch 2 times, most recently from ce82a25 to 87a21d3 Compare April 22, 2019 09:38
@codecov
Copy link

codecov bot commented Apr 22, 2019

Codecov Report

Merging #9505 into master will decrease coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #9505      +/-   ##
==========================================
- Coverage   84.86%   84.86%   -0.01%     
==========================================
  Files         143      143              
  Lines       74044    74056      +12     
==========================================
+ Hits        62835    62845      +10     
- Misses      11209    11211       +2
Impacted Files Coverage Δ
src/dmd/semantic3.d 98.05% <100%> (ø) ⬆️
src/dmd/expression.d 84.3% <100%> (+0.02%) ⬆️
src/dmd/expressionsem.d 90.71% <100%> (ø) ⬆️
src/dmd/typesem.d 89.09% <100%> (+0.02%) ⬆️
src/dmd/opover.d 92.88% <0%> (-0.21%) ⬇️
src/dmd/doc.d 91.35% <0%> (-0.01%) ⬇️
src/dmd/root/speller.d 96.49% <0%> (ø) ⬆️
src/dmd/constfold.d 73.92% <0%> (ø) ⬆️
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 028229d...87a21d3. Read the comment docs.

@codecov
Copy link

codecov bot commented Apr 22, 2019

Codecov Report

Merging #9505 into master will decrease coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #9505      +/-   ##
==========================================
- Coverage   84.86%   84.86%   -0.01%     
==========================================
  Files         143      143              
  Lines       74044    74056      +12     
==========================================
+ Hits        62835    62845      +10     
- Misses      11209    11211       +2
Impacted Files Coverage Δ
src/dmd/semantic3.d 98.05% <100%> (ø) ⬆️
src/dmd/expression.d 84.3% <100%> (+0.02%) ⬆️
src/dmd/expressionsem.d 90.71% <100%> (ø) ⬆️
src/dmd/typesem.d 89.09% <100%> (+0.02%) ⬆️
src/dmd/opover.d 92.88% <0%> (-0.21%) ⬇️
src/dmd/doc.d 91.35% <0%> (-0.01%) ⬇️
src/dmd/root/speller.d 96.49% <0%> (ø) ⬆️
src/dmd/constfold.d 73.92% <0%> (ø) ⬆️
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 028229d...ac57102. Read the comment docs.

@RazvanN7
Copy link
Contributor Author

@andralex all tests green now

@andralex
Copy link
Member

Great, thanks!

@dlang-bot dlang-bot merged commit d13c0ed into dlang:master May 24, 2019
@kinke
Copy link
Contributor

kinke commented Jun 26, 2019

The testcase (compilable/test19754.d) [+ some assertions] yields this AST:

void main()
{
	shared shared(int) x = 0;
	x = 5;
	const const(int) x1 = 0;
	0 = 5; // wtf?
	assert(false);
	immutable immutable(int) x2 = 0;
	0 = 5; // :)
	assert(false);
	return 0;
}

While DMD seems to swallow this (but assertions failing as expected), LDC doesn't allow/expect assigning to an IntegerExp/integer literal (!).

@WalterBright
Copy link
Member

While DMD seems to swallow this

Compiling the code with DMD fails with:

test.d(28): Error: cannot modify constant 0
test.d(31): Error: cannot modify constant 0
test.d(33): Error: cannot return non-void from void function

@kinke
Copy link
Contributor

kinke commented Jun 26, 2019

Glue layer/backend swallows this codegen AST (-vcg-ast).

@kinke
Copy link
Contributor

kinke commented Jun 26, 2019

I tried optimize(WANTvalue, /*keepLvalue*/ exp.op == TOKassign) here:

dmd/src/dmd/expressionsem.d

Lines 8769 to 8770 in bc6bb99

if (e1x.op != TOK.variable)
e1x = e1x.optimize(WANTvalue);

but that's not enough.

kinke added a commit to kinke/dmd that referenced this pull request Sep 12, 2019
kinke added a commit to kinke/dmd that referenced this pull request Sep 27, 2019
kinke added a commit to kinke/dmd that referenced this pull request Sep 27, 2019
kinke added a commit to kinke/dmd that referenced this pull request Sep 27, 2019
kinke added a commit to kinke/dmd that referenced this pull request Jan 17, 2020
kinke added a commit to kinke/dmd that referenced this pull request Jan 17, 2020
kinke added a commit to kinke/dmd that referenced this pull request Jan 17, 2020
kinke added a commit to kinke/dmd that referenced this pull request Aug 19, 2020
kinke added a commit to kinke/dmd that referenced this pull request Aug 19, 2020
kinke added a commit to kinke/dmd that referenced this pull request Aug 19, 2020
kinke added a commit to kinke/dmd that referenced this pull request Aug 19, 2020
kinke added a commit to kinke/dmd that referenced this pull request Aug 19, 2020
kinke added a commit to kinke/dmd that referenced this pull request Aug 19, 2020
kinke added a commit to kinke/dmd that referenced this pull request Aug 27, 2020
kinke added a commit to kinke/dmd that referenced this pull request Aug 27, 2020
kinke added a commit to kinke/dmd that referenced this pull request Aug 27, 2020
kinke added a commit to kinke/dmd that referenced this pull request Sep 2, 2020
kinke added a commit to kinke/dmd that referenced this pull request Sep 2, 2020
kinke added a commit to kinke/dmd that referenced this pull request Sep 2, 2020
kinke added a commit to kinke/dmd that referenced this pull request Sep 2, 2020
kinke added a commit to kinke/dmd that referenced this pull request Sep 2, 2020
kinke added a commit to kinke/dmd that referenced this pull request Sep 2, 2020
kinke added a commit to kinke/dmd that referenced this pull request Sep 2, 2020
kinke added a commit to kinke/dmd that referenced this pull request Sep 25, 2020
kinke added a commit to kinke/dmd that referenced this pull request Sep 25, 2020
kinke added a commit to kinke/dmd that referenced this pull request Sep 25, 2020
dlang-bot added a commit that referenced this pull request Sep 25, 2020
Fix Issue 19754 - Re-apply #9505 and fix it, making isLvalue() logic more restrictive wrt. literals
merged-on-behalf-of: unknown
UplinkCoder pushed a commit to UplinkCoder/dmd that referenced this pull request Jan 5, 2021
UplinkCoder pushed a commit to UplinkCoder/dmd that referenced this pull request Jan 5, 2021
UplinkCoder pushed a commit to UplinkCoder/dmd that referenced this pull request Jan 5, 2021
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.

9 participants