-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[CS] Don't crash when default argument is magic literal and types don't match #26074
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
Conversation
cc @xedin We're crashing because we're using |
@DougGregor or @slavapestov Do you guys know why |
Hmm I guess that might be better than this check. I noticed we do call |
I'd prefer it if we fixed the error by ignoring the invalid default argument entirely in the constraint system or declaration checking instead of handling failure in CSApply. Applying a solution should never fail. Note that checkDefaultArguments() is called for declarations in primary files only. |
I totally I agree, my idea behind asking about checkDefaultArguments was in hope that we could have avoided adding this declaration to overload set, but it seems like we just need to build more constraints to check defaults as part of the solving. All of the |
@xedin What would be the best place to do this? |
@theblixguy Probably while arguments to parameters are being matched in You'll need a new storage in |
Hmm why do we need to do this for magic literal expressions but not others? For example: |
I think we need to do that in general. You might want to start with editor placeholder which has very similar problem, if you look at |
Okay! Could we do the same for functions or function calls i.e. generate a constraint to match (bind/equal?) the argument to the parameter in CSGen? Or do we have to do it in If the only way to do it is in |
Can't really use |
@xedin Hmm we're not actually calling |
@theblixguy @xedin I think this problem only exists with magic literals, not in general. When I define a function like However if you have a magic literal expression, SILGen will emit that expression at the call site, instead of referencing a thunk. So if Checking the expression type every time A better approach would be to use a request evaluator request, to only check magic default arguments once for each unique function referenced. |
@theblixguy I was talking about this place https://github.com/apple/swift/blob/master/lib/Sema/CSSimplify.cpp#L964, there |
@xedin Okay, but if auto allParams = dyn_cast<FuncDecl>(callee)->getParameters()->getArray();
for (auto param : allParams) {
if (param->isDefaultArgument()) {
auto defaultValueExpr = param->getDefaultValue();
auto defaultValueTy = cs.getTypeChecker().typeCheckExpression(defaultValueExpr, param->getDeclContext());
auto paramTy = param->getType();
cs.addConstraint(ConstraintKind::Equal, paramTy, defaultValueTy, locator);
}
} This works for me in the meantime, because the default expression does not have a type yet which is why I need to call typeCheckExpression. |
@theblixguy If there is no argument it means that parameter is going to use a result of default expression as argument which is exactly the situation which requires us to make sure that default expression is valid. We need to generate constraints for default expression and create In this situation we don't really need a type-check call, we can just do |
@xedin Okay, does it look good to you now? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's the right approach, we just need to get some details straight.
@xedin Thanks, I have addressed your comments! |
@xedin I have created a new fix for this to emit a tailored diagnostic. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Couple of small fixes and we should be good to go!
@xedin Thanks, I have addressed your comments! |
@swift-ci please test |
Build failed |
Build failed |
@xedin Can you re-run the CI? Fixed a crash |
@swift-ci please test |
Build failed |
Build failed |
@xedin Can you run the CI again? I addressed a comment from Hamish. Hopefully no more changes required after this! |
@theblixguy could you please squash all of the smaller commits before we merge this? |
Squash all commits into one
@xedin Done! (took way too long for some reason :/) |
Perfect, thank you! |
@swift-ci please test |
Build failed |
Build failed |
@theblixguy Sorry but you'd have to resolve a conflict in CSFix... |
@xedin Done! |
@swift-ci please test |
@swift-ci please test source compatibility |
Build failed |
Build failed |
@xedin Thanks, should I cherry-pick this to 5.1? |
Unfortunately this is a bit too late for 5.1 |
Okay, no problem :) |
This PR fixes a compiler crasher which happened when the default argument was a magic literal (like #line) and the type conversion failed. For example:
Resolves SR-11085.