File: OSLogCompilerDiagnosticsTest.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (92 lines) | stat: -rw-r--r-- 3,059 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// RUN: %target-swift-frontend -swift-version 5 -emit-sil -primary-file %s -o /dev/null -verify
//
// REQUIRES: VENDOR=apple

// Tests for the diagnostics produced by the OSLogOptimization pass that
// performs compile-time analysis and optimization of the new os log APIs.
// Note that many usage errors are caught by the Sema check: ConstantnessSemaDiagnostics.
// The tests here check only those diagnostics that are enforced at the SIL level.

import OSLogTestHelper

func testNonDecimalFormatOptionOnIntegers() {
  _osLogTestHelper("Minimum integer value: \(Int.min, format: .hex)")
  // expected-error @-1 {{Fatal error: Signed integers must be formatted using .decimal}}
}

// Extending OSLogInterpolation (without the constant_evaluable attribute) would be an
// error.
struct A {
  var i: Int
}
extension OSLogInterpolation {
  mutating func appendInterpolation(a: A) {
    self.appendInterpolation(a.i)
  }
}

func testOSLogInterpolationExtension(a: A) {
  _osLogTestHelper("Error at line: \(a: a)")
    // expected-error @-1 {{invalid log message; extending types defined in the os module is not supported}}
    // expected-note @-2 {{'OSLogInterpolation.appendLiteral(_:)' failed evaluation}}
    // expected-note @-3 {{value mutable by an unevaluated instruction is not a constant}}
}

internal enum Color {
  case red
  case blue
}

// Invoking the log calls in unreachable code should not crash the compiler.
func testUnreachableLogCall(c: Color)  {
  let arg = 10
  switch c {
  case .red:
    return
  case .blue:
    return
  default: // expected-warning {{default will never be executed}}
    _osLogTestHelper("Unreachable log call")
    _osLogTestHelper("Unreachable log call with argument \(arg)")
    _osLogTestHelper(
      """
      Unreachable log call with argument and formatting \
      \(arg, align: .right(columns: 10))
      """)
  }
}

// Passing InOut values to the logger should not crash the compiler.
func foo(_ mutableValue: inout String) {
  // expected-note@-1 {{parameter 'mutableValue' is declared 'inout'}}
   _osLogTestHelper("FMFLabelledLocation: initialized with coder \(mutableValue)")
    // expected-error@-1 {{escaping autoclosure captures 'inout' parameter 'mutableValue'}}
    // expected-note@-2 {{pass a copy of 'mutableValue'}}
}

// This is an extension used only for testing a diagnostic that doesn't arise
// normally but may be triggered by changes to the library.
extension OSLogInterpolation {
  @_transparent
  mutating func appendInterpolation(_ c: Color) {
    switch c {
    case .red:
      appendInterpolation(1)
    case .blue:
      appendInterpolation(0)
    }
  }
}

func testUnreachableLogCallComplex(c: Color)  {
  switch c {
  case .red:
    return
  case .blue:
    return
  default: // expected-warning {{default will never be executed}}
    _osLogTestHelper("Some call \(c)")
      // expected-warning@-1 {{os log call will never be executed and may have undiagnosed errors}}
      // expected-error@-2 {{globalStringTablePointer builtin must be used only on string literals}}
  }
}