Skip to content

Commit 8977c1f

Browse files
authored
Merge pull request #31 from p-x9/feature/literal-type-inference
2 parents eacd610 + 3c0377c commit 8977c1f

File tree

4 files changed

+24
-110
lines changed

4 files changed

+24
-110
lines changed

Package.resolved

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
{
22
"pins" : [
3+
{
4+
"identity" : "swift-literal-type-inference",
5+
"kind" : "remoteSourceControl",
6+
"location" : "https://github.com/p-x9/swift-literal-type-inference.git",
7+
"state" : {
8+
"revision" : "d4c97a7e48d3fab0c4575641c9a257602c1d17b7",
9+
"version" : "0.1.0"
10+
}
11+
},
312
{
413
"identity" : "swift-macro-testing",
514
"kind" : "remoteSourceControl",

Package.swift

+10-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ let package = Package(
2020
url: "https://github.com/apple/swift-syntax.git",
2121
from: "509.0.0"
2222
),
23+
.package(
24+
url: "https://github.com/p-x9/swift-literal-type-inference.git",
25+
from: "0.1.0"
26+
),
2327
.package(
2428
url: "https://github.com/pointfreeco/swift-macro-testing.git",
2529
from: "0.2.2"
@@ -44,13 +48,18 @@ let package = Package(
4448
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
4549
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
4650
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
47-
.product(name: "SwiftParserDiagnostics", package: "swift-syntax")
51+
.product(name: "SwiftParserDiagnostics", package: "swift-syntax"),
52+
.product(
53+
name: "LiteralTypeInference",
54+
package: "swift-literal-type-inference"
55+
)
4856
]
4957
),
5058
.testTarget(
5159
name: "AssociatedObjectTests",
5260
dependencies: [
5361
"AssociatedObject",
62+
"AssociatedObjectPlugin",
5463
.product(name: "SwiftSyntax", package: "swift-syntax"),
5564
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
5665
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),

Sources/AssociatedObjectPlugin/AssociatedObjectMacro.swift

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import SwiftSyntax
1010
import SwiftSyntaxBuilder
1111
import SwiftSyntaxMacros
12+
import LiteralTypeInference
1213

1314
public struct AssociatedObjectMacro {}
1415

@@ -41,7 +42,7 @@ extension AssociatedObjectMacro: PeerMacro {
4142
}
4243

4344
let defaultValue = binding.initializer?.value
44-
let type: TypeSyntax? = binding.typeAnnotation?.type ?? defaultValue?.detectedTypeByLiteral
45+
let type: TypeSyntax? = binding.typeAnnotation?.type ?? defaultValue?.inferredType
4546

4647
guard let type else {
4748
// Explicit specification of type is required
@@ -148,9 +149,9 @@ extension AssociatedObjectMacro: AccessorMacro {
148149
if let specifiedType = binding.typeAnnotation?.type {
149150
// TypeAnnotation
150151
type = specifiedType
151-
} else if let detectedType = defaultValue?.detectedTypeByLiteral {
152-
// TypeDetection
153-
type = detectedType
152+
} else if let inferredType = defaultValue?.inferredType {
153+
// infer type of defaultValue
154+
type = inferredType
154155
} else {
155156
// Explicit specification of type is required
156157
context.diagnose(AssociatedObjectMacroDiagnostic.specifyTypeExplicitly.diagnose(at: identifier))

Sources/AssociatedObjectPlugin/Extension/ExprSyntax+.swift

-105
This file was deleted.

0 commit comments

Comments
 (0)