Skip to content

Commit cf30317

Browse files
committed
SILGen: Avoid expanding #line directives with invalid source locations.
Synthesized stub constructors contain `#line` directives and under certain circumstances these directives can have invalid source locations. SILGen would trigger an assert (or invoke UB in non-asserts builds) when attempting to expand these directives into literal values. Check the source location for validity before translating the location to a location in the outermost source file. Resolves rdar://121971741
1 parent 6a86bf3 commit cf30317

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

lib/SILGen/SILGenApply.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,13 +2179,14 @@ buildBuiltinLiteralArgs(SILGenFunction &SGF, SGFContext C,
21792179

21802180
case MagicIdentifierLiteralExpr::Line:
21812181
case MagicIdentifierLiteralExpr::Column: {
2182-
auto Loc = getLocInOutermostSourceFile(SGF.getSourceManager(),
2183-
magicLiteral->getStartLoc());
21842182
unsigned Value = 0;
2185-
if (Loc.isValid()) {
2186-
Value = magicLiteral->getKind() == MagicIdentifierLiteralExpr::Line
2187-
? ctx.SourceMgr.getPresumedLineAndColumnForLoc(Loc).first
2188-
: ctx.SourceMgr.getPresumedLineAndColumnForLoc(Loc).second;
2183+
if (auto Loc = magicLiteral->getStartLoc()) {
2184+
Loc = getLocInOutermostSourceFile(SGF.getSourceManager(), Loc);
2185+
if (Loc.isValid()) {
2186+
Value = magicLiteral->getKind() == MagicIdentifierLiteralExpr::Line
2187+
? ctx.SourceMgr.getPresumedLineAndColumnForLoc(Loc).first
2188+
: ctx.SourceMgr.getPresumedLineAndColumnForLoc(Loc).second;
2189+
}
21892190
}
21902191

21912192
auto silTy = SILType::getBuiltinIntegerLiteralType(ctx);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#import <Foundation/Foundation.h>
2+
3+
NS_ASSUME_NONNULL_BEGIN
4+
5+
@interface ImplClass : NSObject
6+
7+
- (instancetype)init NS_UNAVAILABLE;
8+
- (instancetype)initWithInt:(NSInteger)x;
9+
10+
@end
11+
12+
NS_ASSUME_NONNULL_END

test/SILGen/objc_implementation.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %target-swift-frontend -emit-silgen -import-objc-header %S/Inputs/objc_implementation.h -swift-version 5 %s | %FileCheck %s
2+
3+
// REQUIRES: objc_interop
4+
5+
@_objcImplementation extension ImplClass {
6+
public init(int: Int) {
7+
super.init()
8+
}
9+
10+
// CHECK-LABEL: sil{{.*}} @$sSo9ImplClassC19objc_implementationEABycfc : $@convention(method) (@owned ImplClass) -> @owned ImplClass {
11+
// CHECK: function_ref @$ss25_unimplementedInitializer9className04initD04file4line6columns5NeverOs12StaticStringV_A2JS2utF
12+
// CHECK: } // end sil function '$sSo9ImplClassC19objc_implementationEABycfc'
13+
}

0 commit comments

Comments
 (0)