Skip to content

Commit bd53fe3

Browse files
authored
Revert "[CS] Don't crash when default argument is magic literal and types don't match"
1 parent dcd0893 commit bd53fe3

File tree

10 files changed

+2
-131
lines changed

10 files changed

+2
-131
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,6 @@ ERROR(cannot_convert_default_arg_value,none,
361361
(Type,Type))
362362
ERROR(cannot_convert_default_arg_value_protocol,none,
363363
"default argument value of type %0 does not conform to %1", (Type,Type))
364-
ERROR(default_argument_literal_cannot_convert, none,
365-
"cannot call %0 %1 because default argument of type %2 cannot be "
366-
"converted to type %3", (DescriptiveDeclKind, DeclName, Type, Type))
367364
ERROR(cannot_convert_default_arg_value_nil,none,
368365
"nil default argument value cannot be converted to type %0", (Type))
369366

lib/Sema/CSDiagnostics.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3417,21 +3417,6 @@ bool MissingArgumentsFailure::diagnoseTrailingClosure(ClosureExpr *closure) {
34173417
return true;
34183418
}
34193419

3420-
bool DefaultArgumentTypeMismatch::diagnoseAsError() {
3421-
auto choice = getChoiceFor(getRawAnchor());
3422-
if (!choice.hasValue())
3423-
return false;
3424-
3425-
auto declName = choice.getValue().choice.getName();
3426-
auto declKind = choice.getValue().choice.getDecl()->getDescriptiveKind();
3427-
3428-
emitDiagnostic(getAnchor()->getLoc(),
3429-
diag::default_argument_literal_cannot_convert, declKind,
3430-
declName, FromType, ToType);
3431-
3432-
return true;
3433-
}
3434-
34353420
bool ClosureParamDestructuringFailure::diagnoseAsError() {
34363421
auto *closure = cast<ClosureExpr>(getAnchor());
34373422
auto params = closure->getParameters();

lib/Sema/CSDiagnostics.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,21 +1210,6 @@ class MissingArgumentsFailure final : public FailureDiagnostic {
12101210
bool diagnoseTrailingClosure(ClosureExpr *closure);
12111211
};
12121212

1213-
class DefaultArgumentTypeMismatch final : public FailureDiagnostic {
1214-
using Param = AnyFunctionType::Param;
1215-
1216-
Type FromType;
1217-
Type ToType;
1218-
1219-
public:
1220-
DefaultArgumentTypeMismatch(Expr *root, ConstraintSystem &cs, Type fromType,
1221-
Type toType, ConstraintLocator *locator)
1222-
: FailureDiagnostic(root, cs, locator), FromType(fromType),
1223-
ToType(toType) {}
1224-
1225-
bool diagnoseAsError() override;
1226-
};
1227-
12281213
class OutOfOrderArgumentFailure final : public FailureDiagnostic {
12291214
using ParamBinding = SmallVector<unsigned, 1>;
12301215

lib/Sema/CSFix.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -482,21 +482,6 @@ bool AddMissingArguments::diagnose(Expr *root, bool asNote) const {
482482
return failure.diagnose(asNote);
483483
}
484484

485-
IgnoreDefaultArgumentTypeMismatch *
486-
IgnoreDefaultArgumentTypeMismatch::create(ConstraintSystem &cs, Type fromType,
487-
Type toType,
488-
ConstraintLocator *locator) {
489-
return new (cs.getAllocator())
490-
IgnoreDefaultArgumentTypeMismatch(cs, fromType, toType, locator);
491-
}
492-
493-
bool IgnoreDefaultArgumentTypeMismatch::diagnose(Expr *root,
494-
bool asNote) const {
495-
DefaultArgumentTypeMismatch failure(root, getConstraintSystem(), FromType,
496-
ToType, getLocator());
497-
return failure.diagnose(asNote);
498-
}
499-
500485
AddMissingArguments *
501486
AddMissingArguments::create(ConstraintSystem &cs, FunctionType *funcType,
502487
llvm::ArrayRef<Param> synthesizedArgs,

lib/Sema/CSFix.h

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,6 @@ enum class FixKind : uint8_t {
189189
/// when base is an r-value type.
190190
AllowMutatingMemberOnRValueBase,
191191

192-
/// Fix the type of the default argument
193-
DefaultArgumentTypeMismatch,
194-
195192
/// Allow a single tuple parameter to be matched with N arguments
196193
/// by forming all of the given arguments into a single tuple.
197194
AllowTupleSplatForSingleParameter,
@@ -1023,27 +1020,6 @@ class AddMissingArguments final
10231020
}
10241021
};
10251022

1026-
class IgnoreDefaultArgumentTypeMismatch final : public ConstraintFix {
1027-
Type FromType;
1028-
Type ToType;
1029-
1030-
IgnoreDefaultArgumentTypeMismatch(ConstraintSystem &cs, Type fromType,
1031-
Type toType, ConstraintLocator *locator)
1032-
: ConstraintFix(cs, FixKind::DefaultArgumentTypeMismatch, locator),
1033-
FromType(fromType), ToType(toType) {}
1034-
1035-
public:
1036-
std::string getName() const override {
1037-
return "ignore default argument type mismatch";
1038-
}
1039-
1040-
bool diagnose(Expr *root, bool asNote = false) const override;
1041-
1042-
static IgnoreDefaultArgumentTypeMismatch *create(ConstraintSystem &cs,
1043-
Type fromType, Type toType,
1044-
ConstraintLocator *locator);
1045-
};
1046-
10471023
class MoveOutOfOrderArgument final : public ConstraintFix {
10481024
using ParamBinding = SmallVector<unsigned, 1>;
10491025

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -973,36 +973,9 @@ ConstraintSystem::TypeMatchResult constraints::matchCallArguments(
973973

974974
for (unsigned paramIdx = 0, numParams = parameterBindings.size();
975975
paramIdx != numParams; ++paramIdx){
976-
// If a parameter is unfulfilled, validate the default argument if it is a
977-
// magic literal expression.
978-
if (parameterBindings[paramIdx].empty()) {
979-
if (!callee)
980-
continue;
981-
982-
if (!callee->hasParameterList())
983-
continue;
984-
985-
auto param = getParameterAt(callee, paramIdx);
986-
987-
auto defaultValueExpr = param->getDefaultValue();
988-
989-
if (!(defaultValueExpr &&
990-
isa<MagicIdentifierLiteralExpr>(defaultValueExpr)))
991-
continue;
992-
993-
if (defaultValueExpr->getType())
994-
continue;
995-
996-
cs.generateConstraints(defaultValueExpr, param->getDeclContext());
997-
998-
auto defaultTy = cs.getType(defaultValueExpr);
999-
auto paramTy = param->getType();
1000-
cs.addConstraint(
1001-
ConstraintKind::ArgumentConversion, defaultTy, paramTy,
1002-
locator.withPathElement(ConstraintLocator::DefaultArgument));
1003-
976+
// Skip unfulfilled parameters. There's nothing to do for them.
977+
if (parameterBindings[paramIdx].empty())
1004978
continue;
1005-
}
1006979

1007980
// Determine the parameter type.
1008981
const auto &param = params[paramIdx];
@@ -2628,12 +2601,6 @@ bool ConstraintSystem::repairFailures(
26282601
break;
26292602
}
26302603

2631-
case ConstraintLocator::DefaultArgument: {
2632-
conversionsOrFixes.push_back(IgnoreDefaultArgumentTypeMismatch::create(
2633-
*this, lhs, rhs, getConstraintLocator(anchor, path)));
2634-
break;
2635-
}
2636-
26372604
case ConstraintLocator::TypeParameterRequirement:
26382605
case ConstraintLocator::ConditionalRequirement: {
26392606
// If dependent members are present here it's because
@@ -7482,7 +7449,6 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
74827449
case FixKind::SkipSameTypeRequirement:
74837450
case FixKind::SkipSuperclassRequirement:
74847451
case FixKind::AddMissingArguments:
7485-
case FixKind::DefaultArgumentTypeMismatch:
74867452
case FixKind::SkipUnhandledConstructInFunctionBuilder:
74877453
case FixKind::UsePropertyWrapper:
74887454
case FixKind::UseWrappedValue: {

lib/Sema/ConstraintLocator.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ void ConstraintLocator::Profile(llvm::FoldingSetNodeID &id, Expr *anchor,
5353
case ApplyArgument:
5454
case ApplyFunction:
5555
case FunctionArgument:
56-
case DefaultArgument:
5756
case FunctionResult:
5857
case OptionalPayload:
5958
case Member:
@@ -277,10 +276,6 @@ void ConstraintLocator::dump(SourceManager *sm, raw_ostream &out) {
277276
out << "function argument";
278277
break;
279278

280-
case DefaultArgument:
281-
out << "default argument";
282-
break;
283-
284279
case FunctionResult:
285280
out << "function result";
286281
break;

lib/Sema/ConstraintLocator.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ class ConstraintLocator : public llvm::FoldingSetNode {
6565
GenericParameter,
6666
/// The argument type of a function.
6767
FunctionArgument,
68-
/// The default argument type of a function.
69-
DefaultArgument,
7068
/// The result type of a function.
7169
FunctionResult,
7270
/// A tuple element referenced by position.
@@ -149,7 +147,6 @@ class ConstraintLocator : public llvm::FoldingSetNode {
149147
case ApplyFunction:
150148
case GenericParameter:
151149
case FunctionArgument:
152-
case DefaultArgument:
153150
case FunctionResult:
154151
case OptionalPayload:
155152
case Member:
@@ -247,7 +244,6 @@ class ConstraintLocator : public llvm::FoldingSetNode {
247244
return 0;
248245

249246
case FunctionArgument:
250-
case DefaultArgument:
251247
case FunctionResult:
252248
return IsFunctionConversion;
253249
}

test/decl/func/default-values.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,3 @@ let fooThing5 = Foo(a: 0, d: 1, h: nil) // expected-error {{missing argument for
168168
// Here b = false and g = nil, but we're checking that f doesn't get a default value
169169
let fooThing6 = Foo(a: 0, d: 1, e: 2, h: nil) // expected-error {{missing argument for parameter 'f' in call}}
170170
// expected-note@-29 {{'init(a:b:d:e:f:g:h:)' declared here}}
171-
172-
// SR-11074
173-
174-
func sr_11074(x: Int) {}
175-
func sr_11074(line: String = #line) {} // expected-error {{default argument value of type 'Int' cannot be converted to type 'String'}}
176-
sr_11074() // expected-error {{cannot call global function 'sr_11074(line:)' because default argument of type 'Int' cannot be converted to type 'String'}}
177-
178-
class SR_11074_C { init(line: String = #line) {} } // expected-error {{default argument value of type 'Int' cannot be converted to type 'String'}}
179-
let _ = SR_11074_C() // expected-error {{cannot call initializer 'init(line:)' because default argument of type 'Int' cannot be converted to type 'String'}}

validation-test/compiler_crashers_2_fixed/sr11074.swift

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)