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

fix Issue 21976 - importC: does not distinguish between cast-expression and unary-expression correctly #12627

Merged
merged 1 commit into from Jun 3, 2021

Conversation

WalterBright
Copy link
Member

CParser was confusing a cast expression with a parenthesized expression.

@WalterBright WalterBright added Easy Review ImportC Pertaining to ImportC support labels Jun 3, 2021
@dlang-bot
Copy link
Contributor

dlang-bot commented Jun 3, 2021

Thanks for your pull request, @WalterBright!

Bugzilla references

Auto-close Bugzilla Severity Description
21976 major importC: does not distinguish between cast-expression and unary-expression correctly

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#12627"

@ibuclaw
Copy link
Member

ibuclaw commented Jun 3, 2021

src/dmd/cparse.d Outdated Show resolved Hide resolved
@Geod24 Geod24 removed the auto-merge label Jun 3, 2021
@WalterBright WalterBright changed the title ImportC: distinguish cast from ( expresion ) fix Issue 21976 - importC: does not distinguish between cast-expression and unary-expression correctly Jun 3, 2021
@WalterBright
Copy link
Member Author

This unnecessary ambiguity is one reason why D uses the cast keyword.

@ibuclaw
Copy link
Member

ibuclaw commented Jun 3, 2021

This unnecessary ambiguity is one reason why D uses the cast keyword.

The ambiguity is precisely the reason why a symbol table would be better. The simplicity of C can be used to our advantage here, if an identifier hasn't previously been declared as a typedef typename, then it's not a cast.

@WalterBright
Copy link
Member Author

There is another way. In the semantic code, if (T)*a is seen as a CastExp, the semantic analyzer can look at the T, see it's a variable, and rewrite it as a MulExp.

There's a similar issue with T * b; is it a Declaration or a meaningless Expression? A similar solution can be used.

These might be better than building an extra symbol table just for these two cases.

Anyhow, it's not a problem we need to deal with right now.

@WalterBright
Copy link
Member Author

It's a bit harder problem than just keeping track of which identifiers are typedefs. Local scopes can re-declare identifiers as not-typedefs, so that would have to be accounted for.

@ibuclaw
Copy link
Member

ibuclaw commented Jun 3, 2021

It's a bit harder problem than just keeping track of which identifiers are typedefs. Local scopes can re-declare identifiers as not-typedefs, so that would have to be accounted for.

Sure, I am accounting for that, and it still all works just fine in my head. :-)

typedef int i;       // check scope -> OK, no symbols yet: push to scope symbols
int i = 0;           // check scope -> Error redeclaration in same scope.
{   // push scope
    int i = 0;       // check scope -> OK, current scope has no symbols yet: push to scope symbols
    int a = (i) 42;  // 'i' found in current scope as symbol -> primary expression: error unexpected '42'
}   // pop scope
{   // push scope
    int a = (i) 42;  // 'i' found in outer scope as type -> cast expression
}   // pop scope

I'm sure you can think of more contrived examples, but what I'm imagining is a map using Identifier as the key, and a Type or Dsymbol as the result of the lookup.

ibuclaw
ibuclaw previously requested changes Jun 3, 2021
Copy link
Member

@ibuclaw ibuclaw left a comment

Choose a reason for hiding this comment

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

BTW, your commit message is referencing the wrong issue.

Other than that. I'm fine with pulling this as-is for now. But I'll be looking to improve the situation later, as there's bound to still be a failing test as a result of this.

@WalterBright
Copy link
Member Author

I know how to do the symbol table :-) but I think it may be easier to fix it in the semantic() code.

@WalterBright WalterBright merged commit 23a5071 into dlang:master Jun 3, 2021
@WalterBright WalterBright deleted the parenExp branch June 3, 2021 21:32
UplinkCoder pushed a commit to UplinkCoder/dmd that referenced this pull request Aug 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
5 participants