Skip to content

Commit cc70677

Browse files
authored
Merge pull request #62920 from xedin/rdar-103938899
[Sema] RuntimeMetadata: Make sure that inferred attributes have sourc…
2 parents 3c65ed8 + 94724f9 commit cc70677

File tree

3 files changed

+88
-6
lines changed

3 files changed

+88
-6
lines changed

lib/Sema/TypeCheckRuntimeMetadataAttr.cpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -283,20 +283,25 @@ Expr *SynthesizeRuntimeMetadataAttrGenerator::evaluate(
283283
initArgument = keyPath;
284284
}
285285

286-
auto reprRange = SourceRange();
287-
if (auto *repr = attr->getTypeRepr())
288-
reprRange = repr->getSourceRange();
286+
SourceRange sourceRange;
287+
if (auto *repr = attr->getTypeRepr()) {
288+
sourceRange = repr->getSourceRange();
289+
} else {
290+
sourceRange = SourceRange(
291+
attachedTo->getAttributeInsertionLoc(/*forModifier=*/false));
292+
}
289293

290-
auto typeExpr = TypeExpr::createImplicitHack(reprRange.Start, attrType, ctx);
294+
auto typeExpr =
295+
TypeExpr::createImplicitHack(sourceRange.Start, attrType, ctx);
291296

292297
// Add the initializer argument at the front of the argument list
293298
SmallVector<Argument, 4> newArgs;
294299
newArgs.push_back({/*loc=*/SourceLoc(), ctx.Id_attachedTo, initArgument});
295300
if (auto *attrArgs = attr->getArgs())
296301
newArgs.append(attrArgs->begin(), attrArgs->end());
297302

298-
ArgumentList *argList = ArgumentList::createImplicit(ctx, reprRange.Start,
299-
newArgs, reprRange.End);
303+
ArgumentList *argList = ArgumentList::createImplicit(
304+
ctx, sourceRange.Start, newArgs, sourceRange.End);
300305
Expr *init = CallExpr::createImplicit(ctx, typeExpr, argList);
301306

302307
// result of generator is an optional always.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@runtimeMetadata
2+
public struct Ignore {
3+
public init<T>(attachedTo: T,
4+
fileID: String = #fileID,
5+
line: Int = #line,
6+
column: Int = #column) {}
7+
}
8+
9+
@Ignore
10+
public protocol Ignorable {}

test/SILGen/runtime_attributes.swift

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -enable-experimental-feature RuntimeDiscoverableAttrs -emit-module -o %t -enable-library-evolution %S/Inputs/runtime_metadata_defs.swift
3+
4+
// This uses '-primary-file' to ensure we're conservative with lazy SIL emission.
5+
// RUN: %target-swift-emit-silgen -enable-experimental-feature RuntimeDiscoverableAttrs -primary-file %s -I %t | %FileCheck %s
6+
7+
// REQUIRES: asserts
8+
9+
import runtime_metadata_defs
10+
11+
/// Test that generator has source locations for both explicit and inferred attributes.
12+
13+
// CHECK-LABEL: sil hidden [runtime_accessible] [ossa] @$s18runtime_attributes4TestAaBVmvpfa0A14_metadata_defs6Ignore : $@convention(thin) () -> @out Optional<Ignore>
14+
// CHECK: [[FILE_ID:%.*]] = string_literal utf8 "runtime_attributes/runtime_attributes.swift"
15+
// CHECK: [[STRING_INIT:%.*]] = function_ref @$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC
16+
// CHECK-NEXT: [[FILE_STR:%.*]] = apply [[STRING_INIT]]([[FILE_ID]], {{.*}})
17+
// CHECK: [[LINE_RAW:%.*]] = integer_literal $Builtin.IntLiteral, 25
18+
// CHECK: [[INT_INIT:%.*]] = function_ref @$sSi22_builtinIntegerLiteralSiBI_tcfC
19+
// CHECK-NEXT: [[LINE:%.*]] = apply [[INT_INIT]]([[LINE_RAW]], {{.*}})
20+
// CHECK: [[COLUMN_RAW:%.*]] = integer_literal $Builtin.IntLiteral, 1
21+
// CHECK: [[INT_INIT:%.*]] = function_ref @$sSi22_builtinIntegerLiteralSiBI_tcfC
22+
// CHECK-NEXT: [[COLUMN:%.*]] = apply [[INT_INIT]]([[COLUMN_RAW]], {{.*}})
23+
// CHECK: [[IGNORE_INIT:%.*]] = function_ref @$s21runtime_metadata_defs6IgnoreV10attachedTo6fileID4line6columnACx_SSS2itclufC
24+
// CHECK-NEXT: {{.*}} = apply [[IGNORE_INIT]]<Test.Type>({{.*}}, {{.*}}, [[FILE_STR]], [[LINE]], [[COLUMN]], {{.*}})
25+
struct Test : Ignorable {}
26+
27+
// CHECK-LABEL: sil hidden [runtime_accessible] [ossa] @$s18runtime_attributes8globalFnyycvpfa0A14_metadata_defs6Ignore : $@convention(thin) () -> @out Optional<Ignore>
28+
// CHECK: {{.*}} = string_literal utf8 "runtime_attributes/runtime_attributes.swift"
29+
// CHECK: {{.*}} = integer_literal $Builtin.IntLiteral, 33
30+
// CHECK: {{.*}} = integer_literal $Builtin.IntLiteral, 2
31+
// CHECK: [[IGNORE_INIT:%.*]] = function_ref @$s21runtime_metadata_defs6IgnoreV10attachedTo6fileID4line6columnACx_SSS2itclufC
32+
// CHECK-NEXT: {{.*}} = apply [[IGNORE_INIT]]<() -> ()>({{.*}})
33+
@Ignore func globalFn() {}
34+
35+
struct MemberTests {
36+
// CHECK-LABEL: sil hidden [runtime_accessible] [ossa] @$s18runtime_attributes11MemberTestsV1xSivpfa0A14_metadata_defs6Ignore : $@convention(thin) () -> @out Optional<Ignore>
37+
// CHECK: {{.*}} = string_literal utf8 "runtime_attributes/runtime_attributes.swift"
38+
// CHECK: {{.*}} = integer_literal $Builtin.IntLiteral, 42
39+
// CHECK: {{.*}} = integer_literal $Builtin.IntLiteral, 4
40+
// CHECK: [[IGNORE_INIT:%.*]] = function_ref @$s21runtime_metadata_defs6IgnoreV10attachedTo6fileID4line6columnACx_SSS2itclufC
41+
// CHECK-NEXT: {{.*}} = apply [[IGNORE_INIT]]<WritableKeyPath<MemberTests, Int>>({{.*}})
42+
@Ignore var x: Int = 42
43+
44+
// CHECK-LABEL: sil hidden [runtime_accessible] [ossa] @$s18runtime_attributes11MemberTestsV6instFn_1xSSSi_SaySiGtcvpfa0A14_metadata_defs6Ignore : $@convention(thin) () -> @out Optional<Ignore>
45+
// CHECK: {{.*}} = string_literal utf8 "runtime_attributes/runtime_attributes.swift"
46+
// CHECK: {{.*}} = integer_literal $Builtin.IntLiteral, 50
47+
// CHECK: {{.*}} = integer_literal $Builtin.IntLiteral, 4
48+
// CHECK: [[IGNORE_INIT:%.*]] = function_ref @$s21runtime_metadata_defs6IgnoreV10attachedTo6fileID4line6columnACx_SSS2itclufC
49+
// CHECK-NEXT: {{.*}} = apply [[IGNORE_INIT]]<(MemberTests, Int, [Int]) -> String>({{.*}})
50+
@Ignore func instFn(_: Int, x: [Int]) -> String { "" }
51+
52+
// CHECK-LABEL: sil hidden [runtime_accessible] [ossa] @$s18runtime_attributes11MemberTestsV8staticFn_1ySi_SStSS_SiztcvpZfa0A14_metadata_defs6Ignore : $@convention(thin) () -> @out Optional<Ignore>
53+
// CHECK: {{.*}} = string_literal utf8 "runtime_attributes/runtime_attributes.swift"
54+
// CHECK: {{.*}} = integer_literal $Builtin.IntLiteral, 58
55+
// CHECK: {{.*}} = integer_literal $Builtin.IntLiteral, 4
56+
// CHECK: [[IGNORE_INIT:%.*]] = function_ref @$s21runtime_metadata_defs6IgnoreV10attachedTo6fileID4line6columnACx_SSS2itclufC
57+
// CHECK-NEXT: {{.*}} = apply [[IGNORE_INIT]]<(MemberTests.Type, String, inout Int) -> (Int, String)>({{.*}})
58+
@Ignore static func staticFn(_ x: String, y: inout Int) -> (Int, String) { (42, "") }
59+
60+
// CHECK-LABEL: sil hidden [runtime_accessible] [ossa] @$s18runtime_attributes11MemberTestsV10mutatingFnSiycvpfa0A14_metadata_defs6Ignore : $@convention(thin) () -> @out Optional<Ignore>
61+
// CHECK: {{.*}} = string_literal utf8 "runtime_attributes/runtime_attributes.swift"
62+
// CHECK: {{.*}} = integer_literal $Builtin.IntLiteral, 66
63+
// CHECK: {{.*}} = integer_literal $Builtin.IntLiteral, 4
64+
// CHECK: [[IGNORE_INIT:%.*]] = function_ref @$s21runtime_metadata_defs6IgnoreV10attachedTo6fileID4line6columnACx_SSS2itclufC
65+
// CHECK-NEXT: {{.*}} = apply [[IGNORE_INIT]]<(inout MemberTests) -> Int>({{.*}})
66+
@Ignore mutating func mutatingFn() -> Int { 42 }
67+
}

0 commit comments

Comments
 (0)