Skip to content

Commit c2171ea

Browse files
authored
Merge pull request #84703 from atrick/lifedep-diag-init
Lifetimes: add a diagnostic note for implicit accessors
2 parents b46a095 + 771e9b5 commit c2171ea

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LifetimeDependenceDiagnostics.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ private struct DiagnoseDependence {
284284
diagnose(sourceLoc, .lifetime_value_outside_thunk, thunkSelect, function.name)
285285
}
286286
}
287+
diagnoseImplicitFunction()
287288
reportScope()
288289
// Identify the use point.
289290
if let userSourceLoc = operand.instruction.location.sourceLoc {
@@ -323,6 +324,23 @@ private struct DiagnoseDependence {
323324
}
324325
}
325326
}
327+
328+
func diagnoseImplicitFunction() {
329+
guard let funcLoc = function.location.sourceLoc else {
330+
return
331+
}
332+
if let kindName = {
333+
if function.isInitializer {
334+
return "init"
335+
}
336+
if function.isDeinitializer {
337+
return "deinit"
338+
}
339+
return function.accessorKindName
340+
}() {
341+
diagnose(funcLoc, .implicit_function_note, kindName)
342+
}
343+
}
326344
}
327345

328346
// Identify a best-effort variable declaration based on a defining SIL

SwiftCompilerSources/Sources/SIL/Function.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,18 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash
267267
return StringRef(bridged: bridged.getAccessorName()).string
268268
}
269269

270+
public var isInitializer: Bool {
271+
return bridged.isInitializer()
272+
}
273+
274+
public var isDeinitializer: Bool {
275+
return bridged.isDeinitializer()
276+
}
277+
278+
public var isImplicit: Bool {
279+
return bridged.isImplicit()
280+
}
281+
270282
/// True, if the function runs with a swift 5.1 runtime.
271283
/// Note that this is function specific, because inlinable functions are de-serialized
272284
/// in a client module, which might be compiled with a different deployment target.

include/swift/AST/DiagnosticsSIL.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,8 @@ NOTE(lifetime_outside_scope_use, none,
12401240
NOTE(lifetime_outside_scope_escape, none,
12411241
"this use causes the lifetime-dependent value to escape", ())
12421242

1243+
NOTE(implicit_function_note, none, "error in compiler-generated '%0'", (StringRef))
1244+
12431245
ERROR(noncopyable_shared_case_block_unimplemented, none,
12441246
"matching a non-'Copyable' value using a case label that has multiple patterns is not implemented", ())
12451247

include/swift/SIL/SILBridging.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,9 @@ struct BridgedFunction {
509509
BridgedOwnedString getDebugDescription() const;
510510
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedLocation getLocation() const;
511511
BRIDGED_INLINE bool isAccessor() const;
512+
BRIDGED_INLINE bool isInitializer() const;
513+
BRIDGED_INLINE bool isDeinitializer() const;
514+
BRIDGED_INLINE bool isImplicit() const;
512515
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef getAccessorName() const;
513516
BRIDGED_INLINE bool hasOwnership() const;
514517
BRIDGED_INLINE bool hasLoweredAddresses() const;

include/swift/SIL/SILBridgingImpl.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,21 @@ BridgedStringRef BridgedFunction::getAccessorName() const {
701701
return accessorKindName(accessorDecl->getAccessorKind());
702702
}
703703

704+
bool BridgedFunction::isInitializer() const {
705+
return getFunction()->getDeclRef().isConstructor();
706+
}
707+
708+
bool BridgedFunction::isDeinitializer() const {
709+
return getFunction()->getDeclRef().isDestructor();
710+
}
711+
712+
bool BridgedFunction::isImplicit() const {
713+
if (auto *funcDecl = getFunction()->getDeclRef().getAbstractFunctionDecl()) {
714+
return funcDecl->isImplicit();
715+
}
716+
return false;
717+
}
718+
704719
bool BridgedFunction::hasOwnership() const { return getFunction()->hasOwnership(); }
705720

706721
bool BridgedFunction::hasLoweredAddresses() const { return getFunction()->getModule().useLoweredAddresses(); }

0 commit comments

Comments
 (0)