[ClangImporter] Check for builtin conformance in importNumericLiteral

Make sure the destination type actually conforms to the corresponding
builtin literal protocol before attempting to import it. We may want
to consider allowing any type that conforms to the non-builtin literal
protocol, but I want to keep this patch low risk and just ensure we at
least don't crash for now.

rdar://156524292
This commit is contained in:
Hamish Knight
2025-11-10 15:57:33 +00:00
parent 33e6c0fb6c
commit cdffd55d12
2 changed files with 53 additions and 0 deletions

View File

@@ -125,6 +125,11 @@ static ValueDecl *importNumericLiteral(ClangImporter::Implementation &Impl,
return nullptr; return nullptr;
} }
auto &ctx = DC->getASTContext();
auto *constantTyNominal = constantType->getAnyNominal();
if (!constantTyNominal)
return nullptr;
if (auto *integer = dyn_cast<clang::IntegerLiteral>(parsed)) { if (auto *integer = dyn_cast<clang::IntegerLiteral>(parsed)) {
// Determine the value. // Determine the value.
llvm::APSInt value{integer->getValue(), clangTy->isUnsignedIntegerType()}; llvm::APSInt value{integer->getValue(), clangTy->isUnsignedIntegerType()};
@@ -140,6 +145,16 @@ static ValueDecl *importNumericLiteral(ClangImporter::Implementation &Impl,
} }
} }
// Make sure the destination type actually conforms to the builtin literal
// protocol before attempting to import, otherwise we'll crash since
// `createConstant` expects it to.
// FIXME: We ought to be careful checking conformance here since it can
// result in cycles. Additionally we ought to consider checking for the
// non-builtin literal protocol to allow any ExpressibleByIntegerLiteral
// type to be supported.
if (!ctx.getIntBuiltinInitDecl(constantTyNominal))
return nullptr;
return createMacroConstant(Impl, MI, name, DC, constantType, return createMacroConstant(Impl, MI, name, DC, constantType,
clang::APValue(value), clang::APValue(value),
ConstantConvertKind::None, ConstantConvertKind::None,
@@ -158,6 +173,16 @@ static ValueDecl *importNumericLiteral(ClangImporter::Implementation &Impl,
value.changeSign(); value.changeSign();
} }
// Make sure the destination type actually conforms to the builtin literal
// protocol before attempting to import, otherwise we'll crash since
// `createConstant` expects it to.
// FIXME: We ought to be careful checking conformance here since it can
// result in cycles. Additionally we ought to consider checking for the
// non-builtin literal protocol to allow any ExpressibleByFloatLiteral
// type to be supported.
if (!ctx.getFloatBuiltinInitDecl(constantTyNominal))
return nullptr;
return createMacroConstant(Impl, MI, name, DC, constantType, return createMacroConstant(Impl, MI, name, DC, constantType,
clang::APValue(value), clang::APValue(value),
ConstantConvertKind::None, ConstantConvertKind::None,

View File

@@ -0,0 +1,28 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t
// RUN: %target-swift-frontend -typecheck -verify %t/main.swift -I %t -verify-additional-file %t/cmodule.h
// REQUIRES: objc_interop
// REQUIRES: OS=macosx
//--- cmodule.h
#import <CoreGraphics/CoreGraphics.h>
#define intLiteralCGFloat ((CGFloat)0)
// expected-note@-1 {{invalid numeric literal}}
// expected-note@-2 {{macro 'intLiteralCGFloat' unavailable (cannot import)}}
#define floatLiteralCGFloat ((CGFloat)0.0)
// expected-note@-1 {{invalid numeric literal}}
// expected-note@-2 {{macro 'floatLiteralCGFloat' unavailable (cannot import)}}
//--- module.modulemap
module CModule [system] {
header "cmodule.h"
export *
}
//--- main.swift
import CModule
// Make sure we don't crash when attempting to import these.
_ = intLiteralCGFloat // expected-error {{cannot find 'intLiteralCGFloat' in scope}}
_ = floatLiteralCGFloat // expected-error {{cannot find 'floatLiteralCGFloat' in scope}}