Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reboot fix Issue 23058 - importC: cannot take address inside multi-di… #14094

Merged
merged 1 commit into from
May 11, 2022

Conversation

WalterBright
Copy link
Member

…mensional array at compile time

I was forced to abandon #14044 as it capsized and sank. I wish git had an undo. Sure would be nice.

@WalterBright WalterBright added the Feature:ImportC Pertaining to ImportC support label May 9, 2022
@dlang-bot
Copy link
Contributor

Thanks for your pull request, @WalterBright!

Bugzilla references

Auto-close Bugzilla Severity Description
23058 normal importC: cannot take address inside multi-dimensional array at compile time

Testing this PR locally

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

dub run digger -- build "master + dmd#14094"


void test1()
{
int *p1 = &arr[4][2];
Copy link
Contributor

Choose a reason for hiding this comment

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

This code is technically correct code from the perspective of the C specification. Is it ok to turn it into 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.

It's length+1, which is not technically correct as I recall.

Copy link
Contributor

Choose a reason for hiding this comment

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

I compiled this code with gcc and there's no error. Also, the actual code you are introducing treats C and D code the same.

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right that gcc doesn't give an error for:

int a[10];
int* f() { return &a[100]; }

but clang does with -Wpedantic -std=c11
The C11 6.5.6-8 spec lists such constructs as "undefined behavior". I'm good with diagnosing an error at compile time.

Copy link
Member Author

Choose a reason for hiding this comment

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

There's no reason it should be different for D.

@@ -57,7 +57,7 @@ fail_compilation/fail13902.d(88): Error: Using the result of a comma expression
fail_compilation/fail13902.d(75): Error: returning `& x` escapes a reference to parameter `x`
fail_compilation/fail13902.d(76): Error: returning `&s1.v` escapes a reference to parameter `s1`
fail_compilation/fail13902.d(81): Error: returning `& sa1` escapes a reference to parameter `sa1`
fail_compilation/fail13902.d(82): Error: returning `&sa2[0][0]` escapes a reference to parameter `sa2`
fail_compilation/fail13902.d(82): Error: returning `& sa2` escapes a reference to parameter `sa2`
Copy link

@ghost ghost May 9, 2022

Choose a reason for hiding this comment

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

off topic: why is & formatted sometimes with a trailing space and sometimes not ?

Copy link
Contributor

@dkorpel dkorpel May 9, 2022

Choose a reason for hiding this comment

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

The parser generates an AddrExp for &x, which gets printed as &x as well. Semantic analysis turn an AddrExp into a SymOffExp with offset 0, which gets printed as & x. (See dmd.hdrgen.d for the relevant source code)

Copy link

Choose a reason for hiding this comment

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

shouldn't this be considered as an issue ? What's the point ?

Copy link

Choose a reason for hiding this comment

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

I mean, why the space ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think printing a SymOff expression in user-facing error messages is a bug in itself, since when the offset isn't 0, it prints (& ptr+byteoffset) which doesn't account for pointer arithmetic.

Copy link
Contributor

@dkorpel dkorpel May 9, 2022

Choose a reason for hiding this comment

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

I don't know why the space was added, but from a compiler developer perspective, it is useful to distinguish an AddrExp from a SymOff expression.

Copy link

Choose a reason for hiding this comment

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

that space should be removed IMO.

@WalterBright
Copy link
Member Author

@RazvanN7 ?

@WalterBright WalterBright merged commit db15f03 into dlang:master May 11, 2022
@WalterBright WalterBright deleted the fix23058-2 branch May 11, 2022 22:07
thewilsonator pushed a commit to thewilsonator/dmd that referenced this pull request May 26, 2022
thewilsonator pushed a commit to thewilsonator/dmd that referenced this pull request May 26, 2022
@ibuclaw
Copy link
Member

ibuclaw commented Dec 18, 2022

This PR introduced weird codegen for D array indexing.

auto test(const(ubyte[32])[] a)
{
    return cast(const(ubyte)*)&a[1][0];
}

Before this, frontend passed to the code generator:

return &a[1][0];

With this change, we now see:

return &a[1] + cast(const(ubyte)*)0LU;

Expression ex = new AddrExp(ae1.loc, ae1); // &a[i]
ex.type = ae1.type.pointerTo();

Expression add = new AddExp(ae.loc, ex, new IntegerExp(ae.loc, offset, e.type));
Copy link
Member

Choose a reason for hiding this comment

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

I assume this is the problem - the integer expression is given a pointer type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:ImportC Pertaining to ImportC support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants