Skip to content

Commit 39a25ec

Browse files
authored
Merge pull request #66082 from hamishknight/if-it-is-broke-but-not-directly-fixable-dont-fix-it-5.9
2 parents 5b184b3 + bce39e9 commit 39a25ec

File tree

6 files changed

+111
-3
lines changed

6 files changed

+111
-3
lines changed

lib/AST/DiagnosticEngine.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1264,11 +1264,36 @@ DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic) {
12641264
else if (isNoUsageDiagnostic(diagnostic.getID()))
12651265
Category = "no-usage";
12661266

1267+
auto fixIts = diagnostic.getFixIts();
1268+
if (loc.isValid()) {
1269+
// If the diagnostic is being emitted in a generated buffer, drop the
1270+
// fix-its, as the user will have no way of applying them.
1271+
auto bufferID = SourceMgr.findBufferContainingLoc(loc);
1272+
if (auto generatedInfo = SourceMgr.getGeneratedSourceInfo(bufferID)) {
1273+
switch (generatedInfo->kind) {
1274+
case GeneratedSourceInfo::ExpressionMacroExpansion:
1275+
case GeneratedSourceInfo::FreestandingDeclMacroExpansion:
1276+
case GeneratedSourceInfo::AccessorMacroExpansion:
1277+
case GeneratedSourceInfo::MemberAttributeMacroExpansion:
1278+
case GeneratedSourceInfo::MemberMacroExpansion:
1279+
case GeneratedSourceInfo::PeerMacroExpansion:
1280+
case GeneratedSourceInfo::ConformanceMacroExpansion:
1281+
case GeneratedSourceInfo::PrettyPrinted:
1282+
fixIts = {};
1283+
break;
1284+
case GeneratedSourceInfo::ReplacedFunctionBody:
1285+
// A replaced function body is for user-written code, so fix-its are
1286+
// still valid.
1287+
break;
1288+
}
1289+
}
1290+
}
1291+
12671292
return DiagnosticInfo(
12681293
diagnostic.getID(), loc, toDiagnosticKind(behavior),
12691294
diagnosticStringFor(diagnostic.getID(), getPrintDiagnosticNames()),
12701295
diagnostic.getArgs(), Category, getDefaultDiagnosticLoc(),
1271-
/*child note info*/ {}, diagnostic.getRanges(), diagnostic.getFixIts(),
1296+
/*child note info*/ {}, diagnostic.getRanges(), fixIts,
12721297
diagnostic.isChildNote());
12731298
}
12741299

lib/Parse/ParseDecl.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,6 @@ Parser::parseSourceFileViaASTGen(SmallVectorImpl<ASTNode> &items,
344344
Optional<DiagnosticTransaction> &transaction,
345345
bool suppressDiagnostics) {
346346
#if SWIFT_SWIFT_PARSER
347-
using ParsingFlags = SourceFile::ParsingFlags;
348-
const auto parsingOpts = SF.getParsingOptions();
349347
const auto &langOpts = Context.LangOpts;
350348

351349
// We only need to do parsing if we either have ASTGen enabled, or want the

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,25 @@ public struct DefineStructWithUnqualifiedLookupMacro: DeclarationMacro {
12931293
}
12941294
}
12951295

1296+
public struct AddMemberWithFixIt: MemberMacro {
1297+
public static func expansion<
1298+
Declaration: DeclGroupSyntax, Context: MacroExpansionContext
1299+
>(
1300+
of node: AttributeSyntax,
1301+
providingMembersOf declaration: Declaration,
1302+
in context: Context
1303+
) throws -> [DeclSyntax] {
1304+
[
1305+
"""
1306+
func foo() {
1307+
var x = 0
1308+
_ = x
1309+
}
1310+
"""
1311+
]
1312+
}
1313+
}
1314+
12961315
extension TupleExprElementListSyntax {
12971316
/// Retrieve the first element with the given label.
12981317
func first(labeled name: String) -> Element? {

test/Macros/macro_fixits.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// REQUIRES: swift_swift_parser
2+
3+
// This test ensures we don't emit fix-its in generated code, such as macro
4+
// expansion buffers.
5+
6+
// RUN: %empty-directory(%t)
7+
8+
// RUN: %host-build-swift -emit-library %S/Inputs/syntax_macro_definitions.swift -o %t/%target-library-name(MacroDefinition) -module-name MacroDefinition -swift-version 5 -g -no-toolchain-stdlib-rpath
9+
10+
// RUN: %target-swift-frontend -typecheck %s -load-plugin-library %t/%target-library-name(MacroDefinition) -swift-version 5 -serialize-diagnostics-path %t/diags.dia -fixit-all -emit-fixits-path %t/fixits.json
11+
12+
// RUN: %FileCheck %s --check-prefix FIXITS-JSON < %t/fixits.json
13+
14+
// FIXITS-JSON: [
15+
// FIXITS-JSON-NEXT: ]
16+
17+
// RUN: c-index-test -read-diagnostics %t/diags.dia 2>&1 | %FileCheck -check-prefix DIAGS %s
18+
19+
// DIAGS: warning: variable 'x' was never mutated; consider changing to 'let' constant
20+
// DIAGS-NEXT: Number FIXITs = 0
21+
22+
@attached(member, names: arbitrary)
23+
public macro addMemberWithFixIt() = #externalMacro(module: "MacroDefinition", type: "AddMemberWithFixIt")
24+
25+
@addMemberWithFixIt
26+
struct S {}

test/SourceKit/Macros/diags.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ func foo() {
1616
@Invalid
1717
struct Bad {}
1818

19+
@attached(member, names: arbitrary)
20+
public macro addMemberWithFixIt() = #externalMacro(module: "MacroDefinition", type: "AddMemberWithFixIt")
21+
22+
// Make sure we don't emit the fix-it for the member added by this macro.
23+
@addMemberWithFixIt
24+
struct S {}
25+
1926
// REQUIRES: swift_swift_parser
2027

2128
// RUN: %empty-directory(%t)

test/SourceKit/Macros/diags.swift.response

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@
4444
key.buffer_name: diags.swift
4545
},
4646
key.buffer_name: "@__swiftmacro_9MacroUser3Bad7InvalidfMp_.swift"
47+
},
48+
{
49+
key.buffer_text: "func foo() {\n var x = 0\n _ = x\n}",
50+
key.original_location: {
51+
key.offset: 748,
52+
key.length: 0,
53+
key.buffer_name: diags.swift
54+
},
55+
key.buffer_name: "@__swiftmacro_9MacroUser1S18addMemberWithFixItfMm_.swift"
4756
}
4857
],
4958
key.diagnostics: [
@@ -175,6 +184,30 @@
175184
}
176185
]
177186
},
187+
{
188+
key.line: 2,
189+
key.column: 7,
190+
key.filepath: "@__swiftmacro_9MacroUser1S18addMemberWithFixItfMm_.swift",
191+
key.severity: source.diagnostic.severity.warning,
192+
key.id: "variable_never_mutated",
193+
key.description: "variable 'x' was never mutated; consider changing to 'let' constant",
194+
key.diagnostics: [
195+
{
196+
key.line: 24,
197+
key.column: 1,
198+
key.filepath: diags.swift,
199+
key.severity: source.diagnostic.severity.note,
200+
key.id: "in_macro_expansion",
201+
key.description: "in expansion of macro 'addMemberWithFixIt' here",
202+
key.ranges: [
203+
{
204+
key.offset: 738,
205+
key.length: 11
206+
}
207+
]
208+
}
209+
]
210+
},
178211
{
179212
key.line: 3,
180213
key.column: 17,

0 commit comments

Comments
 (0)