File: OSLogCompilerDiagnosticsTest.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (92 lines) | stat: -rw-r--r-- 3,059 bytes parent folder | download | duplicates (2)
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}}
  }
}