Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Sema] RuntimeMetadata: Make sure that inferred attributes have sourc… #62920

Merged
merged 1 commit into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions lib/Sema/TypeCheckRuntimeMetadataAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,20 +283,25 @@ Expr *SynthesizeRuntimeMetadataAttrGenerator::evaluate(
initArgument = keyPath;
}

auto reprRange = SourceRange();
if (auto *repr = attr->getTypeRepr())
reprRange = repr->getSourceRange();
SourceRange sourceRange;
if (auto *repr = attr->getTypeRepr()) {
sourceRange = repr->getSourceRange();
} else {
sourceRange = SourceRange(
attachedTo->getAttributeInsertionLoc(/*forModifier=*/false));
}

auto typeExpr = TypeExpr::createImplicitHack(reprRange.Start, attrType, ctx);
auto typeExpr =
TypeExpr::createImplicitHack(sourceRange.Start, attrType, ctx);

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

ArgumentList *argList = ArgumentList::createImplicit(ctx, reprRange.Start,
newArgs, reprRange.End);
ArgumentList *argList = ArgumentList::createImplicit(
ctx, sourceRange.Start, newArgs, sourceRange.End);
Expr *init = CallExpr::createImplicit(ctx, typeExpr, argList);

// result of generator is an optional always.
Expand Down
10 changes: 10 additions & 0 deletions test/SILGen/Inputs/runtime_metadata_defs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@runtimeMetadata
public struct Ignore {
public init<T>(attachedTo: T,
fileID: String = #fileID,
line: Int = #line,
column: Int = #column) {}
}

@Ignore
public protocol Ignorable {}
67 changes: 67 additions & 0 deletions test/SILGen/runtime_attributes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -enable-experimental-feature RuntimeDiscoverableAttrs -emit-module -o %t -enable-library-evolution %S/Inputs/runtime_metadata_defs.swift

// This uses '-primary-file' to ensure we're conservative with lazy SIL emission.
// RUN: %target-swift-emit-silgen -enable-experimental-feature RuntimeDiscoverableAttrs -primary-file %s -I %t | %FileCheck %s

// REQUIRES: asserts

import runtime_metadata_defs

/// Test that generator has source locations for both explicit and inferred attributes.

// CHECK-LABEL: sil hidden [runtime_accessible] [ossa] @$s18runtime_attributes4TestAaBVmvpfa0A14_metadata_defs6Ignore : $@convention(thin) () -> @out Optional<Ignore>
// CHECK: [[FILE_ID:%.*]] = string_literal utf8 "runtime_attributes/runtime_attributes.swift"
// CHECK: [[STRING_INIT:%.*]] = function_ref @$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC
// CHECK-NEXT: [[FILE_STR:%.*]] = apply [[STRING_INIT]]([[FILE_ID]], {{.*}})
// CHECK: [[LINE_RAW:%.*]] = integer_literal $Builtin.IntLiteral, 25
// CHECK: [[INT_INIT:%.*]] = function_ref @$sSi22_builtinIntegerLiteralSiBI_tcfC
// CHECK-NEXT: [[LINE:%.*]] = apply [[INT_INIT]]([[LINE_RAW]], {{.*}})
// CHECK: [[COLUMN_RAW:%.*]] = integer_literal $Builtin.IntLiteral, 1
// CHECK: [[INT_INIT:%.*]] = function_ref @$sSi22_builtinIntegerLiteralSiBI_tcfC
// CHECK-NEXT: [[COLUMN:%.*]] = apply [[INT_INIT]]([[COLUMN_RAW]], {{.*}})
// CHECK: [[IGNORE_INIT:%.*]] = function_ref @$s21runtime_metadata_defs6IgnoreV10attachedTo6fileID4line6columnACx_SSS2itclufC
// CHECK-NEXT: {{.*}} = apply [[IGNORE_INIT]]<Test.Type>({{.*}}, {{.*}}, [[FILE_STR]], [[LINE]], [[COLUMN]], {{.*}})
struct Test : Ignorable {}

// CHECK-LABEL: sil hidden [runtime_accessible] [ossa] @$s18runtime_attributes8globalFnyycvpfa0A14_metadata_defs6Ignore : $@convention(thin) () -> @out Optional<Ignore>
// CHECK: {{.*}} = string_literal utf8 "runtime_attributes/runtime_attributes.swift"
// CHECK: {{.*}} = integer_literal $Builtin.IntLiteral, 33
// CHECK: {{.*}} = integer_literal $Builtin.IntLiteral, 2
// CHECK: [[IGNORE_INIT:%.*]] = function_ref @$s21runtime_metadata_defs6IgnoreV10attachedTo6fileID4line6columnACx_SSS2itclufC
// CHECK-NEXT: {{.*}} = apply [[IGNORE_INIT]]<() -> ()>({{.*}})
@Ignore func globalFn() {}

struct MemberTests {
// CHECK-LABEL: sil hidden [runtime_accessible] [ossa] @$s18runtime_attributes11MemberTestsV1xSivpfa0A14_metadata_defs6Ignore : $@convention(thin) () -> @out Optional<Ignore>
// CHECK: {{.*}} = string_literal utf8 "runtime_attributes/runtime_attributes.swift"
// CHECK: {{.*}} = integer_literal $Builtin.IntLiteral, 42
// CHECK: {{.*}} = integer_literal $Builtin.IntLiteral, 4
// CHECK: [[IGNORE_INIT:%.*]] = function_ref @$s21runtime_metadata_defs6IgnoreV10attachedTo6fileID4line6columnACx_SSS2itclufC
// CHECK-NEXT: {{.*}} = apply [[IGNORE_INIT]]<WritableKeyPath<MemberTests, Int>>({{.*}})
@Ignore var x: Int = 42

// CHECK-LABEL: sil hidden [runtime_accessible] [ossa] @$s18runtime_attributes11MemberTestsV6instFn_1xSSSi_SaySiGtcvpfa0A14_metadata_defs6Ignore : $@convention(thin) () -> @out Optional<Ignore>
// CHECK: {{.*}} = string_literal utf8 "runtime_attributes/runtime_attributes.swift"
// CHECK: {{.*}} = integer_literal $Builtin.IntLiteral, 50
// CHECK: {{.*}} = integer_literal $Builtin.IntLiteral, 4
// CHECK: [[IGNORE_INIT:%.*]] = function_ref @$s21runtime_metadata_defs6IgnoreV10attachedTo6fileID4line6columnACx_SSS2itclufC
// CHECK-NEXT: {{.*}} = apply [[IGNORE_INIT]]<(MemberTests, Int, [Int]) -> String>({{.*}})
@Ignore func instFn(_: Int, x: [Int]) -> String { "" }

// CHECK-LABEL: sil hidden [runtime_accessible] [ossa] @$s18runtime_attributes11MemberTestsV8staticFn_1ySi_SStSS_SiztcvpZfa0A14_metadata_defs6Ignore : $@convention(thin) () -> @out Optional<Ignore>
// CHECK: {{.*}} = string_literal utf8 "runtime_attributes/runtime_attributes.swift"
// CHECK: {{.*}} = integer_literal $Builtin.IntLiteral, 58
// CHECK: {{.*}} = integer_literal $Builtin.IntLiteral, 4
// CHECK: [[IGNORE_INIT:%.*]] = function_ref @$s21runtime_metadata_defs6IgnoreV10attachedTo6fileID4line6columnACx_SSS2itclufC
// CHECK-NEXT: {{.*}} = apply [[IGNORE_INIT]]<(MemberTests.Type, String, inout Int) -> (Int, String)>({{.*}})
@Ignore static func staticFn(_ x: String, y: inout Int) -> (Int, String) { (42, "") }

// CHECK-LABEL: sil hidden [runtime_accessible] [ossa] @$s18runtime_attributes11MemberTestsV10mutatingFnSiycvpfa0A14_metadata_defs6Ignore : $@convention(thin) () -> @out Optional<Ignore>
// CHECK: {{.*}} = string_literal utf8 "runtime_attributes/runtime_attributes.swift"
// CHECK: {{.*}} = integer_literal $Builtin.IntLiteral, 66
// CHECK: {{.*}} = integer_literal $Builtin.IntLiteral, 4
// CHECK: [[IGNORE_INIT:%.*]] = function_ref @$s21runtime_metadata_defs6IgnoreV10attachedTo6fileID4line6columnACx_SSS2itclufC
// CHECK-NEXT: {{.*}} = apply [[IGNORE_INIT]]<(inout MemberTests) -> Int>({{.*}})
@Ignore mutating func mutatingFn() -> Int { 42 }
}