Skip to content

Commit 3c731d8

Browse files
committed
[TypeChecker] Add a flag to disable Double<->CGFloat implicit conversion
1 parent 826c145 commit 3c731d8

File tree

6 files changed

+20
-0
lines changed

6 files changed

+20
-0
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,9 @@ namespace swift {
579579

580580
/// See \ref FrontendOptions.PrintFullConvention
581581
bool PrintFullConvention = false;
582+
583+
/// Disallow Double and CGFloat types to be used interchangeably.
584+
bool DisableImplicitDoubleCGFloatConversion = false;
582585
};
583586

584587
/// Options for controlling the behavior of the Clang importer.

include/swift/Option/FrontendOptions.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ def enable_resilience : Flag<["-"], "enable-resilience">,
244244
// HIDDEN FLAGS
245245
let Flags = [FrontendOption, NoDriverOption, HelpHidden] in {
246246

247+
def disable_implicit_double_cgfloat_conversion : Flag<["-"], "disable-implicit-double-cgfloat-conversion">,
248+
HelpText<"Disable implicit conversion between Double and CGFloat types">;
249+
247250
def debug_constraints : Flag<["-"], "debug-constraints">,
248251
HelpText<"Debug the constraint-based type checker">;
249252

include/swift/Sema/ConstraintSystem.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,10 @@ enum class ConstraintSystemFlags {
14051405
/// calling conventions, say due to Clang attributes such as
14061406
/// `__attribute__((ns_consumed))`.
14071407
UseClangFunctionTypes = 0x80,
1408+
1409+
/// Disallow to use Double and CGFloat interchangeably through means of
1410+
/// an implicit value conversion.
1411+
DisableImplicitDoubleCGFloatConversion = 0x100,
14081412
};
14091413

14101414
/// Options that affect the constraint system as a whole.

lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,9 @@ static bool ParseTypeCheckerArgs(TypeCheckerOptions &Opts, ArgList &Args,
809809
Opts.DebugConstraintSolver |= Args.hasArg(OPT_debug_constraints);
810810
Opts.DebugGenericSignatures |= Args.hasArg(OPT_debug_generic_signatures);
811811

812+
Opts.DisableImplicitDoubleCGFloatConversion |=
813+
Args.hasArg(OPT_disable_implicit_double_cgfloat_conversion);
814+
812815
for (const Arg *A : Args.filtered(OPT_debug_constraints_on_line)) {
813816
unsigned line;
814817
if (StringRef(A->getValue()).getAsInteger(/*radix*/ 10, line)) {

lib/Sema/CSSimplify.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5087,6 +5087,10 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
50875087
nominal1->getDecl() != nominal2->getDecl() &&
50885088
((nominal1->isCGFloatType() || nominal2->isCGFloatType()) &&
50895089
(nominal1->isDoubleType() || nominal2->isDoubleType()))) {
5090+
if (Options.contains(
5091+
ConstraintSystemFlags::DisableImplicitDoubleCGFloatConversion))
5092+
return getTypeMatchFailure(locator);
5093+
50905094
// Support implicit Double<->CGFloat conversions only for
50915095
// something which could be directly represented in the AST
50925096
// e.g. argument-to-parameter, contextual conversions etc.

lib/Sema/ConstraintSystem.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ ConstraintSystem::ConstraintSystem(DeclContext *dc,
8888
}
8989
if (Context.LangOpts.UseClangFunctionTypes)
9090
Options |= ConstraintSystemFlags::UseClangFunctionTypes;
91+
92+
if (Context.TypeCheckerOpts.DisableImplicitDoubleCGFloatConversion)
93+
Options |= ConstraintSystemFlags::DisableImplicitDoubleCGFloatConversion;
9194
}
9295

9396
ConstraintSystem::~ConstraintSystem() {

0 commit comments

Comments
 (0)