@@ -35,28 +35,28 @@ extension DiagnosticsEngine {
3535
3636 public func emit(
3737 error: String ,
38- location: DiagnosticLocation = UnknownLocation . location
38+ location: DiagnosticLocation ? = nil
3939 ) {
4040 emit ( . error( error) , location: location)
4141 }
4242
4343 public func emit(
4444 warning: String ,
45- location: DiagnosticLocation = UnknownLocation . location
45+ location: DiagnosticLocation ? = nil
4646 ) {
4747 emit ( . warning( warning) , location: location)
4848 }
4949
5050 public func emit(
5151 note: String ,
52- location: DiagnosticLocation = UnknownLocation . location
52+ location: DiagnosticLocation ? = nil
5353 ) {
5454 emit ( . note( note) , location: location)
5555 }
5656
5757 public func emit(
5858 remark: String ,
59- location: DiagnosticLocation = UnknownLocation . location
59+ location: DiagnosticLocation ? = nil
6060 ) {
6161 emit ( . remark( remark) , location: location)
6262 }
@@ -67,7 +67,7 @@ extension DiagnosticsEngine {
6767 /// Otherwise, they will be emitted as AnyDiagnostic.
6868 public func emit(
6969 _ error: Swift . Error ,
70- location: DiagnosticLocation = UnknownLocation . location
70+ location: DiagnosticLocation ? = nil
7171 ) {
7272 if let diagnosticData = error as? DiagnosticData {
7373 emit ( . error( diagnosticData) , location: location)
@@ -81,26 +81,31 @@ extension DiagnosticsEngine {
8181 /// Emit a diagnostic data convertible instance.
8282 public func emit(
8383 _ convertible: DiagnosticDataConvertible ,
84- location: DiagnosticLocation = UnknownLocation . location
84+ location: DiagnosticLocation ? = nil
8585 ) {
8686 emit ( . error( convertible. diagnosticData) , location: location)
8787 }
8888
89+ @discardableResult
90+ public func with< T> ( location: DiagnosticLocation , _ closure: ( DiagnosticsEngine ) -> T ) -> T {
91+ let innerDiagnostics = DiagnosticsEngine ( handlers: [ self . emit] , defaultLocation: location)
92+ return closure ( innerDiagnostics)
93+ }
94+
8995 /// Wrap a throwing closure, returning an optional value and
9096 /// emitting any thrown errors.
9197 ///
9298 /// - Parameters:
9399 /// - closure: Closure to wrap.
94100 /// - Returns: Returns the return value of the closure wrapped
95101 /// into an optional. If the closure throws, nil is returned.
96- public func wrap< T> (
97- with constuctLocation: @autoclosure ( ) -> ( DiagnosticLocation ) = UnknownLocation . location,
98- _ closure: ( ) throws -> T
99- ) -> T ? {
102+ public func wrap< T> ( _ closure: ( ) throws -> T ) -> T ? {
100103 do {
101104 return try closure ( )
105+ } catch Diagnostics . fatalError {
106+ return nil
102107 } catch {
103- emit ( error, location : constuctLocation ( ) )
108+ emit ( error)
104109 return nil
105110 }
106111 }
@@ -113,20 +118,65 @@ extension DiagnosticsEngine {
113118 /// - Returns: Returns true if the wrapped closure did not throw
114119 /// and false otherwise.
115120 @discardableResult
116- public func wrap(
117- with constuctLocation: @autoclosure ( ) -> ( DiagnosticLocation ) = UnknownLocation . location,
118- _ closure: ( ) throws -> Void
119- ) -> Bool {
121+ public func wrap( _ closure: ( ) throws -> Void ) -> Bool {
120122 do {
121123 try closure ( )
122124 return true
125+ } catch Diagnostics . fatalError {
126+ return false
123127 } catch {
124- emit ( error, location : constuctLocation ( ) )
128+ emit ( error)
125129 return false
126130 }
127131 }
128132}
129133
134+ extension Optional where Wrapped == DiagnosticsEngine {
135+ public func emit(
136+ error: String ,
137+ location: DiagnosticLocation ? = nil
138+ ) throws {
139+ if case let diagnostics? = self {
140+ diagnostics. emit ( . error( error) , location: location)
141+ } else {
142+ throw Diagnostics . fatalError
143+ }
144+ }
145+
146+ public func emit(
147+ _ error: Swift . Error ,
148+ location: DiagnosticLocation ? = nil
149+ ) throws {
150+ if case let diagnostics? = self {
151+ diagnostics. emit ( error, location: location)
152+ } else {
153+ throw Diagnostics . fatalError
154+ }
155+ }
156+
157+ public func emit(
158+ _ convertible: DiagnosticDataConvertible ,
159+ location: DiagnosticLocation ? = nil
160+ ) throws {
161+ if case let diagnostics? = self {
162+ diagnostics. emit ( . error( convertible. diagnosticData) , location: location)
163+ } else {
164+ throw Diagnostics . fatalError
165+ }
166+ }
167+
168+ public func emit(
169+ _ message: Diagnostic . Message ,
170+ location: DiagnosticLocation ? = nil
171+ ) throws {
172+ if case let diagnostics? = self {
173+ diagnostics. emit ( message, location: location)
174+ } else if message. behavior == . error {
175+ throw Diagnostics . fatalError
176+ }
177+ }
178+ }
179+
130180/// Namespace for representing diagnostic location of a package.
131181public enum PackageLocation {
132182
0 commit comments