@@ -1019,6 +1019,33 @@ static void checkInheritedDefaultValueRestrictions(ParamDecl *PD) {
1019
1019
}
1020
1020
}
1021
1021
1022
+ static bool checkExpressionMacroDefaultValueRestrictions (ParamDecl *param) {
1023
+ assert (param->getDefaultArgumentKind () ==
1024
+ DefaultArgumentKind::ExpressionMacro);
1025
+ auto &ctx = param->getASTContext ();
1026
+ auto *initExpr = param->getStructuralDefaultExpr ();
1027
+ assert (initExpr);
1028
+
1029
+ // Prohibit default argument that is a non-built-in macro to avoid confusion,
1030
+ // unless the experimental feature flag is set.
1031
+ if (!ctx.LangOpts .hasFeature (Feature::ExpressionMacroDefaultArguments)) {
1032
+ ctx.Diags .diagnose (initExpr->getLoc (), diag::macro_as_default_argument);
1033
+ return false ;
1034
+ }
1035
+
1036
+ auto macroExpansionExpr = cast<MacroExpansionExpr>(initExpr);
1037
+ // only allow arguments that are literals
1038
+ auto args = macroExpansionExpr->getArgs ();
1039
+ for (auto arg : *args)
1040
+ if (!dyn_cast<LiteralExpr>(arg.getExpr ())) {
1041
+ ctx.Diags .diagnose (
1042
+ arg.getExpr ()->getLoc (),
1043
+ diag::macro_as_default_argument_arguments_must_be_literal);
1044
+ return false ;
1045
+ }
1046
+ return true ;
1047
+ }
1048
+
1022
1049
void TypeChecker::notePlaceholderReplacementTypes (Type writtenType,
1023
1050
Type inferredType) {
1024
1051
assert (writtenType && inferredType &&
@@ -1132,20 +1159,12 @@ Expr *DefaultArgumentExprRequest::evaluate(Evaluator &evaluator,
1132
1159
auto *initExpr = param->getStructuralDefaultExpr ();
1133
1160
assert (initExpr);
1134
1161
1135
- // Prohibit default argument that is a non-built-in macro to avoid confusion,
1136
- auto macroExpansionExpr = dyn_cast<MacroExpansionExpr>(initExpr);
1137
- if (macroExpansionExpr) {
1138
- // unless the experimental feature flag is set.
1139
- if (!ctx.LangOpts .hasFeature (Feature::ExpressionMacroDefaultArguments)) {
1140
- ctx.Diags .diagnose (initExpr->getLoc (), diag::macro_as_default_argument);
1141
- return new (ctx) ErrorExpr (initExpr->getSourceRange (), ErrorType::get (ctx));
1142
- }
1143
- auto args = macroExpansionExpr->getArgs ();
1144
- if (!args->empty ()) {
1145
- ctx.Diags .diagnose (args->getLoc (), diag::macro_as_default_argument_has_arguments);
1146
- return new (ctx) ErrorExpr (initExpr->getSourceRange (), ErrorType::get (ctx));
1147
- }
1148
- }
1162
+ auto isMacroExpansionExpr =
1163
+ param->getDefaultArgumentKind () == DefaultArgumentKind::ExpressionMacro;
1164
+
1165
+ if (isMacroExpansionExpr &&
1166
+ !checkExpressionMacroDefaultValueRestrictions (param))
1167
+ return new (ctx) ErrorExpr (initExpr->getSourceRange (), ErrorType::get (ctx));
1149
1168
1150
1169
// If the param has an error type, there's no point type checking the default
1151
1170
// expression, unless we are type checking for code completion, in which case
@@ -1163,7 +1182,7 @@ Expr *DefaultArgumentExprRequest::evaluate(Evaluator &evaluator,
1163
1182
}
1164
1183
1165
1184
// Expression macro default arguments are checked at caller side
1166
- if (macroExpansionExpr )
1185
+ if (isMacroExpansionExpr )
1167
1186
return initExpr;
1168
1187
1169
1188
// Walk the checked initializer and contextualize any closures
0 commit comments