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

Match if expression is const, to get the proper error message #16086

Closed
wants to merge 1 commit into from

Conversation

ryuukk
Copy link
Contributor

@ryuukk ryuukk commented Jan 23, 2024

Consider:

struct Stuff {

    int opApply(scope int delegate(const (Stuff)*) dg) 
    {
        return 0;
    }

};
extern(C) void main()
{
    const(Stuff)* stuff;
    foreach(const (Stuff)* it;*stuff)
    {}
}

Before:

_.d(13): Error: cannot uniquely infer `foreach` argument types

After:

_.d(13): Error: mutable method `_.Stuff.opApply` is not callable using a `const` object
_.d(4):        Consider adding `const` or `inout` here

Fixes bugzilla Issue 19114 - cannot uniquely infer foreach argument types is not a descriptive message
Fixes bugzilla Issue 24353 - Misleading error for foreach when opApply has wrong qualifier

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @ryuukk! 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 coverage diff by visiting the details link of the codecov check)
  • 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

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

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

Copy link
Contributor

@thewilsonator thewilsonator left a comment

Choose a reason for hiding this comment

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

you will need to update test cases

@dkorpel
Copy link
Contributor

dkorpel commented Jan 24, 2024

Please add "Fix bugzilla issue 19114" and "Fix bugzilla issue 24353" to the commit message so the bot links the issues.

@ryuukk
Copy link
Contributor Author

ryuukk commented Jan 24, 2024

Is the fix correct at least?

@@ -1719,7 +1719,14 @@ private FuncDeclaration findBestOpApplyMatch(Expression ethis, FuncDeclaration f
if (f.isThis())
{
if (!MODimplicitConv(mod, tf.mod))
{
if (mod & MODFlags.const_)
Copy link
Contributor

@RazvanN7 RazvanN7 Jan 25, 2024

Choose a reason for hiding this comment

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

The findBestOpApplyMatch iterates through all of the overloads of opApply and selects the best match (if there is one) or errors in case you have more than one match. In case there is no match, the function returns null. This patch modifies the code so that if the instance type does not match the opApply instance type and the instance type is const then the considered opApply is chosen as the best match. This is wrong for multiple reasons:

  • you are simply making const a particular case here; what happens with immutable, shared, etc.?
  • you are considering a function that is not a match to be a match; this might break other parts of the compiler that call this function.
  • this is not going to work properly when you have multiple opApply's that fail for similar reasons; you are just going to pick the last failing as the best match.
  • consider the case when having both a const and a mutable opApply; you will obtain a wrong ambiguity error.

I think that the only solution here is to:

  • call findBestOpApplyMatch.
  • if there is a single opApply defined and it's not a match, then try calling it anyway (resolveFuncCall) to obtain a proper error message.
  • if you have multiple opApplys and none of them are a match, then the current error message is fine.

So, the fix is not in this function.

Copy link
Contributor

@RazvanN7 RazvanN7 left a comment

Choose a reason for hiding this comment

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

This is not the correct fix.

@RazvanN7
Copy link
Contributor

@ryuukk are you willing to implement the proper solution?

@ryuukk
Copy link
Contributor Author

ryuukk commented Jan 29, 2024

Not right now, I have too much to do on my current project

@dkorpel
Copy link
Contributor

dkorpel commented Jan 31, 2024

When you want to continue, please re-open / open a new PR.

@dkorpel dkorpel closed this Jan 31, 2024
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