Skip to content

Remove "unsafe" keyword from expressions when printing inlinable code #79462

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

Merged
merged 1 commit into from
Feb 20, 2025
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
30 changes: 26 additions & 4 deletions lib/ASTGen/Sources/ASTGen/CompilerBuildConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import ASTBridging
import SwiftDiagnostics
@_spi(Compiler) import SwiftIfConfig
import SwiftParser
import SwiftSyntax
@_spi(ExperimentalLanguageFeatures) import SwiftParser
@_spi(ExperimentalLanguageFeatures) import SwiftSyntax

/// A build configuration that uses the compiler's ASTContext to answer
/// queries.
Expand Down Expand Up @@ -517,7 +517,11 @@ public func extractInlinableText(
sourceText: BridgedStringRef
) -> BridgedStringRef {
let textBuffer = UnsafeBufferPointer<UInt8>(start: sourceText.data, count: sourceText.count)
var parser = Parser(textBuffer)
var parser = Parser(
textBuffer,
swiftVersion: Parser.SwiftVersion(from: astContext),
experimentalFeatures: Parser.ExperimentalFeatures(from: astContext)
)
let syntax = SourceFileSyntax.parse(from: &parser)

let configuration = CompilerBuildConfiguration(
Expand All @@ -531,6 +535,24 @@ public func extractInlinableText(
retainFeatureCheckIfConfigs: true
).result

// Remove "unsafe" expressions.
let inlineableSyntax = syntaxWithoutInactive.withoutUnsafeExpressions

// Remove comments and return the result.
return allocateBridgedString(syntaxWithoutInactive.descriptionWithoutCommentsAndSourceLocations)
return allocateBridgedString(inlineableSyntax.descriptionWithoutCommentsAndSourceLocations)
}

/// Used by withoutUnsafeExpressions to remove "unsafe" expressions from
/// a syntax tree.
fileprivate class RemoveUnsafeExprSyntaxRewriter: SyntaxRewriter {
override func visit(_ node: UnsafeExprSyntax) -> ExprSyntax {
return node.expression.with(\.leadingTrivia, node.leadingTrivia)
}
}

extension SyntaxProtocol {
/// Return a new syntax tree with all "unsafe" expressions removed.
var withoutUnsafeExpressions: Syntax {
RemoveUnsafeExprSyntaxRewriter().rewrite(self)
}
}
34 changes: 34 additions & 0 deletions test/Unsafe/module-interface.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %s -module-name UserModule -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe
// RUN: %target-swift-typecheck-module-from-interface(%t.swiftinterface) -module-name UserModule
// RUN: %FileCheck %s < %t.swiftinterface

// REQUIRES: swift_feature_AllowUnsafeAttribute
// REQUIRES: swift_feature_WarnUnsafe

// CHECK: #if compiler(>=5.3) && $AllowUnsafeAttribute
// CHECK: @unsafe public func getIntUnsafely() -> Swift.Int
// CHECK: #else
// CHECK: public func getIntUnsafely() -> Swift.Int
// CHECK: #endif
@unsafe public func getIntUnsafely() -> Int { 0 }

// CHECK: @inlinable public func useUnsafeCode()
@inlinable public func useUnsafeCode() {
// CHECK-NOT: unsafe
print( unsafe getIntUnsafely())
}

// CHECK: public protocol P
public protocol P {
func f()
}

// CHECK: public struct X : @unsafe UserModule.P
public struct X: @unsafe P {
// CHECK: #if compiler(>=5.3) && $AllowUnsafeAttribute
// CHECK: @unsafe public func f()
// CHECK: #else
// CHECK: public func f()
// CHECK: #endif
@unsafe public func f() { }
}