Skip to content

Commit b3a9656

Browse files
committed
Merge pull request #2957 from jckarter/optional-unwrap-source-loc
2 parents 45b1ea1 + b198bed commit b3a9656

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

lib/SILGen/SILGenConvert.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,46 @@ static CanType getOptionalValueType(SILType optType,
157157
return generic.getGenericArgs()[0];
158158
}
159159

160+
static void emitSourceLocationArgs(SILGenFunction &gen,
161+
SILLocation loc,
162+
ManagedValue (&args)[4]) {
163+
auto &ctx = gen.getASTContext();
164+
auto sourceLoc = loc.getSourceLoc();
165+
166+
StringRef filename = "";
167+
unsigned line = 0;
168+
if (sourceLoc.isValid()) {
169+
unsigned bufferID = ctx.SourceMgr.findBufferContainingLoc(sourceLoc);
170+
filename = ctx.SourceMgr.getIdentifierForBuffer(bufferID);
171+
line = ctx.SourceMgr.getLineAndColumn(sourceLoc).first;
172+
}
173+
174+
bool isASCII = true;
175+
for (unsigned char c : filename) {
176+
if (c > 127) {
177+
isASCII = false;
178+
break;
179+
}
180+
}
181+
182+
auto wordTy = SILType::getBuiltinWordType(ctx);
183+
auto i1Ty = SILType::getBuiltinIntegerType(1, ctx);
184+
185+
// File
186+
SILValue literal = gen.B.createStringLiteral(loc, filename,
187+
StringLiteralInst::Encoding::UTF8);
188+
args[0] = ManagedValue::forUnmanaged(literal);
189+
// File length
190+
literal = gen.B.createIntegerLiteral(loc, wordTy, filename.size());
191+
args[1] = ManagedValue::forUnmanaged(literal);
192+
// File is ascii
193+
literal = gen.B.createIntegerLiteral(loc, i1Ty, isASCII);
194+
args[2] = ManagedValue::forUnmanaged(literal);
195+
// Line
196+
literal = gen.B.createIntegerLiteral(loc, wordTy, line);
197+
args[3] = ManagedValue::forUnmanaged(literal);
198+
}
199+
160200
void SILGenFunction::emitPreconditionOptionalHasValue(SILLocation loc,
161201
SILValue optional) {
162202
OptionalTypeKind OTK;
@@ -181,7 +221,10 @@ void SILGenFunction::emitPreconditionOptionalHasValue(SILLocation loc,
181221
// Call the standard library implementation of _diagnoseUnexpectedNilOptional.
182222
if (auto diagnoseFailure =
183223
getASTContext().getDiagnoseUnexpectedNilOptional(nullptr)) {
184-
emitApplyOfLibraryIntrinsic(loc, diagnoseFailure, {}, {},
224+
ManagedValue args[4];
225+
emitSourceLocationArgs(*this, loc, args);
226+
227+
emitApplyOfLibraryIntrinsic(loc, diagnoseFailure, {}, args,
185228
SGFContext());
186229
}
187230

lib/SILGen/SILGenExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2033,7 +2033,7 @@ getMagicFunctionString(SILGenFunction &gen) {
20332033

20342034
RValue RValueEmitter::
20352035
visitMagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr *E, SGFContext C) {
2036-
ASTContext &Ctx = SGF.SGM.M.getASTContext();
2036+
ASTContext &Ctx = SGF.getASTContext();
20372037
SILType Ty = SGF.getLoweredLoadableType(E->getType());
20382038
SourceLoc Loc;
20392039

stdlib/public/core/Optional.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,16 @@ extension Optional : CustomReflectable {
286286

287287
@_transparent
288288
public // COMPILER_INTRINSIC
289-
func _diagnoseUnexpectedNilOptional() {
289+
func _diagnoseUnexpectedNilOptional(_filenameStart: Builtin.RawPointer,
290+
_filenameLength: Builtin.Word,
291+
_filenameIsASCII: Builtin.Int1,
292+
_line: Builtin.Word) {
290293
_preconditionFailure(
291-
"unexpectedly found nil while unwrapping an Optional value")
294+
"unexpectedly found nil while unwrapping an Optional value",
295+
file: StaticString(_start: _filenameStart,
296+
utf8CodeUnitCount: _filenameLength,
297+
isASCII: _filenameIsASCII),
298+
line: UInt(_line))
292299
}
293300

294301
public func == <T: Equatable> (lhs: T?, rhs: T?) -> Bool {

test/SILGen/optional_lvalue.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// CHECK-LABEL: sil hidden @_TF15optional_lvalue22assign_optional_lvalueFTRGSqSi_Si_T_
44
// CHECK: [[SHADOW:%.*]] = alloc_box $Optional<Int>
55
// CHECK: [[PB:%.*]] = project_box [[SHADOW]]
6-
// CHECK: [[PRECOND:%.*]] = function_ref @_TFs30_diagnoseUnexpectedNilOptionalFT_T_
7-
// CHECK: apply [[PRECOND]]()
6+
// CHECK: [[PRECOND:%.*]] = function_ref @_TFs30_diagnoseUnexpectedNilOptional
7+
// CHECK: apply [[PRECOND]](
88
// CHECK: [[PAYLOAD:%.*]] = unchecked_take_enum_data_addr [[PB]] : $*Optional<Int>, #Optional.some!enumelt.1
99
// CHECK: assign {{%.*}} to [[PAYLOAD]]
1010
func assign_optional_lvalue(_ x: inout Int?, _ y: Int) {
@@ -14,8 +14,8 @@ func assign_optional_lvalue(_ x: inout Int?, _ y: Int) {
1414
// CHECK-LABEL: sil hidden @_TF15optional_lvalue17assign_iuo_lvalueFTRGSQSi_Si_T_
1515
// CHECK: [[SHADOW:%.*]] = alloc_box $ImplicitlyUnwrappedOptional<Int>
1616
// CHECK: [[PB:%.*]] = project_box [[SHADOW]]
17-
// CHECK: [[PRECOND:%.*]] = function_ref @_TFs30_diagnoseUnexpectedNilOptionalFT_T_
18-
// CHECK: apply [[PRECOND]]()
17+
// CHECK: [[PRECOND:%.*]] = function_ref @_TFs30_diagnoseUnexpectedNilOptional
18+
// CHECK: apply [[PRECOND]](
1919
// CHECK: [[PAYLOAD:%.*]] = unchecked_take_enum_data_addr [[PB]] : $*ImplicitlyUnwrappedOptional<Int>, #ImplicitlyUnwrappedOptional.some!enumelt.1
2020
// CHECK: assign {{%.*}} to [[PAYLOAD]]
2121
func assign_iuo_lvalue(_ x: inout Int!, _ y: Int) {

0 commit comments

Comments
 (0)