Skip to content

Commit 1a84ca2

Browse files
committed
Constrain the DeclReferenceExprSyntax special case a bit
1 parent 37ce7d9 commit 1a84ca2

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

Sources/TestingMacros/Support/ConditionArgumentParsing.swift

+11-1
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,20 @@ private final class _ContextInserter<C, M>: SyntaxRewriter where C: MacroExpansi
321321
//
322322
// These sorts of expressions are relatively rare, so we'll allow the bug
323323
// for the sake of better diagnostics in the common case.
324-
if let memberAccessExpr = node.parent?.as(MemberAccessExprSyntax.self),
324+
if node.argumentNames == nil,
325+
let memberAccessExpr = node.parent?.as(MemberAccessExprSyntax.self),
325326
ExprSyntax(node) == memberAccessExpr.base,
326327
let functionCallExpr = memberAccessExpr.parent?.as(FunctionCallExprSyntax.self),
327328
ExprSyntax(memberAccessExpr) == functionCallExpr.calledExpression {
329+
// If the base name is an identifier and its first character is uppercase,
330+
// it is presumably a type name or module name, so don't expand it. (This
331+
// isn't a great heuristic, but it hopefully minimizes the module name
332+
// problem above.)
333+
if case .identifier = node.baseName.tokenKind,
334+
let firstCharacter = node.baseName.textWithoutBackticks.first, firstCharacter.isUppercase {
335+
return ExprSyntax(node)
336+
}
337+
328338
return _rewrite(
329339
MemberAccessExprSyntax(
330340
base: node.trimmed,

Tests/TestingMacrosTests/ConditionMacroTests.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ struct ConditionMacroTests {
3131
##"#expect(2 > 1)"##:
3232
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in __ec(2 > 1, 0x0) }, sourceCode: [0x0: "2 > 1"], comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()"##,
3333
##"#expect(((true || false) && true) || Bool.random())"##:
34-
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in __ec(__ec((__ec(__ec((__ec(__ec(true, 0xf7a) || __ec(false, 0x877a), 0x77a)), 0x7a) && __ec(true, 0x10003a), 0x3a)), 0x2) || __ec(__ec(Bool.self, 0xe000000).random(), 0x2000000), 0x0) }, sourceCode: [0x0: "((true || false) && true) || Bool.random()", 0x2: "((true || false) && true)", 0x3a: "(true || false) && true", 0x7a: "(true || false)", 0x77a: "true || false", 0xf7a: "true", 0x877a: "false", 0x10003a: "true", 0x2000000: "Bool.random()", 0xe000000: "Bool"], comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()"##,
34+
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in __ec(__ec((__ec(__ec((__ec(__ec(true, 0xf7a) || __ec(false, 0x877a), 0x77a)), 0x7a) && __ec(true, 0x10003a), 0x3a)), 0x2) || __ec(Bool.random(), 0x2000000), 0x0) }, sourceCode: [0x0: "((true || false) && true) || Bool.random()", 0x2: "((true || false) && true)", 0x3a: "(true || false) && true", 0x7a: "(true || false)", 0x77a: "true || false", 0xf7a: "true", 0x877a: "false", 0x10003a: "true", 0x2000000: "Bool.random()"], comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()"##,
3535
##"#expect(9 > 8 && 7 > 6, "Some comment")"##:
3636
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in __ec(__ec(9 > 8, 0x2) && __ec(7 > 6, 0x400), 0x0) }, sourceCode: [0x0: "9 > 8 && 7 > 6", 0x2: "9 > 8", 0x400: "7 > 6"], comments: ["Some comment"], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()"##,
3737
##"#expect("a" == "b")"##:
3838
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in __ec.__cmp({ $0 == $1 }, 0x0, "a", 0x2, "b", 0x200) }, sourceCode: [0x0: #""a" == "b""#], comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()"##,
3939
##"#expect(!Bool.random())"##:
40-
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in __ec(!__ec(__ec(Bool.self, 0x1c).random(), 0x4), 0x0) }, sourceCode: [0x0: "!Bool.random()", 0x4: "Bool.random()", 0x1c: "Bool"], comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()"##,
40+
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in __ec(!__ec(Bool.random(), 0x4), 0x0) }, sourceCode: [0x0: "!Bool.random()", 0x4: "Bool.random()"], comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()"##,
4141
##"#expect((true && false))"##:
4242
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in __ec((__ec(__ec(true, 0x3c) && __ec(false, 0x21c), 0x1c)), 0x0) }, sourceCode: [0x0: "(true && false)", 0x1c: "true && false", 0x3c: "true", 0x21c: "false"], comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()"##,
4343
##"#expect(try x())"##:
@@ -112,13 +112,13 @@ struct ConditionMacroTests {
112112
##"#require(2 > 1)"##:
113113
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in try Testing.__requiringTry(__ec(2 > 1, 0x0)) }, sourceCode: [0x0: "2 > 1"], comments: [], isRequired: true, sourceLocation: Testing.SourceLocation.__here()).__required()"##,
114114
##"#require(((true || false) && true) || Bool.random())"##:
115-
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in try Testing.__requiringTry(__ec(__ec((__ec(__ec((__ec(__ec(true, 0xf7a) || __ec(false, 0x877a), 0x77a)), 0x7a) && __ec(true, 0x10003a), 0x3a)), 0x2) || __ec(__ec(Bool.self, 0xe000000).random(), 0x2000000), 0x0)) }, sourceCode: [0x0: "((true || false) && true) || Bool.random()", 0x2: "((true || false) && true)", 0x3a: "(true || false) && true", 0x7a: "(true || false)", 0x77a: "true || false", 0xf7a: "true", 0x877a: "false", 0x10003a: "true", 0x2000000: "Bool.random()", 0xe000000: "Bool"], comments: [], isRequired: true, sourceLocation: Testing.SourceLocation.__here()).__required()"##,
115+
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in try Testing.__requiringTry(__ec(__ec((__ec(__ec((__ec(__ec(true, 0xf7a) || __ec(false, 0x877a), 0x77a)), 0x7a) && __ec(true, 0x10003a), 0x3a)), 0x2) || __ec(Bool.random(), 0x2000000), 0x0)) }, sourceCode: [0x0: "((true || false) && true) || Bool.random()", 0x2: "((true || false) && true)", 0x3a: "(true || false) && true", 0x7a: "(true || false)", 0x77a: "true || false", 0xf7a: "true", 0x877a: "false", 0x10003a: "true", 0x2000000: "Bool.random()"], comments: [], isRequired: true, sourceLocation: Testing.SourceLocation.__here()).__required()"##,
116116
##"#require(9 > 8 && 7 > 6, "Some comment")"##:
117117
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in try Testing.__requiringTry(__ec(__ec(9 > 8, 0x2) && __ec(7 > 6, 0x400), 0x0)) }, sourceCode: [0x0: "9 > 8 && 7 > 6", 0x2: "9 > 8", 0x400: "7 > 6"], comments: ["Some comment"], isRequired: true, sourceLocation: Testing.SourceLocation.__here()).__required()"##,
118118
##"#require("a" == "b")"##:
119119
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in try Testing.__requiringTry(__ec.__cmp({ $0 == $1 }, 0x0, "a", 0x2, "b", 0x200)) }, sourceCode: [0x0: #""a" == "b""#], comments: [], isRequired: true, sourceLocation: Testing.SourceLocation.__here()).__required()"##,
120120
##"#require(!Bool.random())"##:
121-
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in try Testing.__requiringTry(__ec(!__ec(__ec(Bool.self, 0x1c).random(), 0x4), 0x0)) }, sourceCode: [0x0: "!Bool.random()", 0x4: "Bool.random()", 0x1c: "Bool"], comments: [], isRequired: true, sourceLocation: Testing.SourceLocation.__here()).__required()"##,
121+
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in try Testing.__requiringTry(__ec(!__ec(Bool.random(), 0x4), 0x0)) }, sourceCode: [0x0: "!Bool.random()", 0x4: "Bool.random()"], comments: [], isRequired: true, sourceLocation: Testing.SourceLocation.__here()).__required()"##,
122122
##"#require((true && false))"##:
123123
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in try Testing.__requiringTry(__ec((__ec(__ec(true, 0x3c) && __ec(false, 0x21c), 0x1c)), 0x0)) }, sourceCode: [0x0: "(true && false)", 0x1c: "true && false", 0x3c: "true", 0x21c: "false"], comments: [], isRequired: true, sourceLocation: Testing.SourceLocation.__here()).__required()"##,
124124
##"#require(try x())"##:

0 commit comments

Comments
 (0)