Необязательные свойства вызвали ошибку sh в протоколе Swift - PullRequest
0 голосов
/ 20 июня 2020

Я написал код для тестирования дополнительных свойств в Swift, но код вылетал с сообщениями об ошибках, которые я не мог понять. Я использовал онлайн-компилятор, который использует Swift Release 5.01.

FYI, сообщение об ошибке добавлено в конец сообщения. Это может быть немного длинным и запутанным для чтения. Спасибо за ваше время.

Пожалуйста, помогите мне, указав на проблему в приведенном ниже коде:

import Foundation

@objc protocol Flier {
    @objc optional var song : String {get}
    @objc optional func sing (_ lyrics: String) -> String
}

class BirdA : Flier {

}
class BirdB : Flier {
    var song = "tweet"

    func sing (_ lyrics: String) -> String {
        print(lyrics)

        return lyrics + ", " + lyrics + "!"
    }
}

let f1 : Flier = BirdA()
let f2 : Flier = BirdB()

if let song = f1.song {
  print(song)
} else {
  print("f1.song has no song.")
}

let s = f2.sing?("Chirp")
print(s!)

Спасибо!

Сообщение об ошибке:

alloc_global @$s4main1sSSSgvp                   // id: %94
  %95 = global_addr @$s4main1sSSSgvp : $*Optional<String> // users: %142, %168, %130
  %96 = load_borrow %10 : $*Flier                 // users: %134, %116, %97
  %97 = open_existential_ref %96 : $Flier to $@opened("73064B52-B2D1-11EA-895B-0242AC120003") Flier // users: %103, %98
  %98 = copy_value %97 : $@opened("73064B52-B2D1-11EA-895B-0242AC120003") Flier // users: %133, %115, %102, %100
  %99 = alloc_stack $Optional<@callee_guaranteed (@guaranteed String) -> @owned String> // users: %132, %114, %112, %110, %108, %106
  dynamic_method_br %98 : $@opened("73064B52-B2D1-11EA-895B-0242AC120003") Flier, #Flier.sing!1.foreign, bb9, bb10 // id: %100

// %101                                           // user: %103
bb9(%101 : @trivial $@convention(objc_method) (NSString, @opened("73064B52-B2D1-11EA-895B-0242AC120003") Flier) -> @autoreleased NSString): // Preds: bb8
  %102 = copy_value %98 : $@opened("73064B52-B2D1-11EA-895B-0242AC120003") Flier // user: %103
  %103 = partial_apply [callee_guaranteed] %101(%102) : $@convention(objc_method) (NSString, @opened("73064B52-B2D1-11EA-895B-0242AC120003") Flier) -> @autoreleased NSString // type-defs: %97; user: %105
  // function_ref thunk for @escaping @callee_guaranteed (@unowned NSString) -> (@owned NSString)
  %104 = function_ref @$s10Foundation8NSStringCACIegyo_S2SIeggo_TR : $@convention(thin) (@guaranteed String, @guaranteed @callee_guaranteed (NSString) -> @owned NSString) -> @owned String // user: %105
  %105 = partial_apply [callee_guaranteed] %104(%103) : $@convention(thin) (@guaranteed String, @guaranteed @callee_guaranteed (NSString) -> @owned NSString) -> @owned String // user: %107
  %106 = init_enum_data_addr %99 : $*Optional<@callee_guaranteed (@guaranteed String) -> @owned String>, #Optional.some!enumelt.1 // user: %107
  store %105 to [init] %106 : $*@callee_guaranteed (@guaranteed String) -> @owned String // id: %107
  inject_enum_addr %99 : $*Optional<@callee_guaranteed (@guaranteed String) -> @owned String>, #Optional.some!enumelt.1 // id: %108
  br bb11                                         // id: %109

bb10:                                             // Preds: bb8
  inject_enum_addr %99 : $*Optional<@callee_guaranteed (@guaranteed String) -> @owned String>, #Optional.none!enumelt // id: %110
  br bb11                                         // id: %111

bb11:                                             // Preds: bb10 bb9
  %112 = load [take] %99 : $*Optional<@callee_guaranteed (@guaranteed String) -> @owned String> // user: %113
  switch_enum %112 : $Optional<@callee_guaranteed (@guaranteed String) -> @owned String>, case #Optional.some!enumelt.1: bb13, case #Optional.none!enumelt: bb12 // id: %113

bb12:                                             // Preds: bb11
  dealloc_stack %99 : $*Optional<@callee_guaranteed (@guaranteed String) -> @owned String> // id: %114
  destroy_value %98 : $@opened("73064B52-B2D1-11EA-895B-0242AC120003") Flier // id: %115
  end_borrow %96 : $Flier                         // id: %116
  br bb17                                         // id: %117

// %118                                           // users: %131, %125
bb13(%118 : @owned $@callee_guaranteed (@guaranteed String) -> @owned String): // Preds: bb11
  %119 = string_literal utf8 "Chirp"              // user: %124
  %120 = integer_literal $Builtin.Word, 5         // user: %124
  %121 = integer_literal $Builtin.Int1, -1        // user: %124
  %122 = metatype $@thin String.Type              // user: %124
  // function_ref String.init(_builtinStringLiteral:utf8CodeUnitCount:isASCII:)
  %123 = function_ref @$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String // user: %124
  %124 = apply %123(%119, %120, %121, %122) : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String // users: %128, %126
  %125 = begin_borrow %118 : $@callee_guaranteed (@guaranteed String) -> @owned String // users: %127, %126
  %126 = apply %125(%124) : $@callee_guaranteed (@guaranteed String) -> @owned String // user: %129
  end_borrow %125 : $@callee_guaranteed (@guaranteed String) -> @owned String // id: %127
  destroy_value %124 : $String                    // id: %128
  %129 = enum $Optional<String>, #Optional.some!enumelt.1, %126 : $String // user: %130
  store %129 to [init] %95 : $*Optional<String>   // id: %130
  destroy_value %118 : $@callee_guaranteed (@guaranteed String) -> @owned String // id: %131
  dealloc_stack %99 : $*Optional<@callee_guaranteed (@guaranteed String) -> @owned String> // id: %132
  destroy_value %98 : $@opened("73064B52-B2D1-11EA-895B-0242AC120003") Flier // id: %133
  end_borrow %96 : $Flier                         // id: %134
  br bb14                                         // id: %135

bb14:                                             // Preds: bb17 bb13
  %136 = integer_literal $Builtin.Word, 1         // user: %138
  // function_ref _allocateUninitializedArray<A>(_:)
  %137 = function_ref @$ss27_allocateUninitializedArrayySayxG_BptBwlF : $@convention(thin) <τ_0_0> (Builtin.Word) -> (@owned Array<τ_0_0>, Builtin.RawPointer) // user: %138
  %138 = apply %137<Any>(%136) : $@convention(thin) <τ_0_0> (Builtin.Word) -> (@owned Array<τ_0_0>, Builtin.RawPointer) // user: %139
  (%139, %140) = destructure_tuple %138 : $(Array<Any>, Builtin.RawPointer) // users: %164, %161, %141
  %141 = pointer_to_address %140 : $Builtin.RawPointer to [strict] $*Any // user: %154
  %142 = load [copy] %95 : $*Optional<String>     // user: %143compiler exit status 254

  switch_enum %142 : $Optional<String>, case #Optional.some!enumelt.1: bb16, case #Optional.none!enumelt: bb15 // id: %143

bb15:                                             // Preds: bb14
  %144 = string_literal utf8 "main.swift"         // user: %151
  %145 = integer_literal $Builtin.Word, 10        // user: %151
  %146 = integer_literal $Builtin.Int1, -1        // user: %151
  %147 = integer_literal $Builtin.Word, 31        // user: %151
  %148 = integer_literal $Builtin.Word, 8
  %149 = integer_literal $Builtin.Int1, 0         // user: %151
  // function_ref _diagnoseUnexpectedNilOptional(_filenameStart:_filenameLength:_filenameIsASCII:_line:_isImplicitUnwrap:)
  %150 = function_ref @$ss30_diagnoseUnexpectedNilOptional14_filenameStart01_E6Length01_E7IsASCII5_line17_isImplicitUnwrapyBp_BwBi1_BwBi1_tF : $@convention(thin) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, Builtin.Word, Builtin.Int1) -> () // user: %151
  %151 = apply %150(%144, %145, %146, %147, %149) : $@convention(thin) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, Builtin.Word, Builtin.Int1) -> ()
  unreachable                                     // id: %152

// %153                                           // user: %155
bb16(%153 : @owned $String):                      // Preds: bb14
  %154 = init_existential_addr %141 : $*Any, $String // user: %155
  store %153 to [init] %154 : $*String            // id: %155
  // function_ref default argument 1 of print(_:separator:terminator:)
  %156 = function_ref @$ss5print_9separator10terminatoryypd_S2StFfA0_ : $@convention(thin) () -> @owned String // user: %157
  %157 = apply %156() : $@convention(thin) () -> @owned String // users: %163, %161
  // function_ref default argument 2 of print(_:separator:terminator:)
  %158 = function_ref @$ss5print_9separator10terminatoryypd_S2StFfA1_ : $@convention(thin) () -> @owned String // user: %159
  %159 = apply %158() : $@convention(thin) () -> @owned String // users: %162, %161
  // function_ref print(_:separator:terminator:)
  %160 = function_ref @$ss5print_9separator10terminatoryypd_S2StF : $@convention(thin) (@guaranteed Array<Any>, @guaranteed String, @guaranteed String) -> () // user: %161
  %161 = apply %160(%139, %157, %159) : $@convention(thin) (@guaranteed Array<Any>, @guaranteed String, @guaranteed String) -> ()
  destroy_value %159 : $String                    // id: %162
  destroy_value %157 : $String                    // id: %163
  destroy_value %139 : $Array<Any>                // id: %164
  %165 = integer_literal $Builtin.Int32, 0        // user: %166
  %166 = struct $Int32 (%165 : $Builtin.Int32)    // user: %167
  return %166 : $Int32                            // id: %167

bb17:                                             // Preds: bb12
  inject_enum_addr %95 : $*Optional<String>, #Optional.none!enumelt // id: %168
  br bb14                                         // id: %169
} // end sil function 'main'

Stack dump:
0.  Program arguments: /usr/bin/swift -frontend -c -primary-file main.swift -target x86_64-unknown-linux-gnu -disable-objc-interop -color-diagnostics -module-name main -o /tmp/main-62d5b0.o 
1.  While verifying SIL function "@main".
/usr/bin/swift[0x423d3b4]
/usr/bin/swift[0x423b13e]
/usr/bin/swift[0x423d572]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x12890)[0x7f8b1fd94890]
/lib/x86_64-linux-gnu/libc.so.6(gsignal+0xc7)[0x7f8b1e1f7e97]
/lib/x86_64-linux-gnu/libc.so.6(abort+0x141)[0x7f8b1e1f9801]
/usr/bin/swift[0x1349968]
/usr/bin/swift[0x1351323]
/usr/bin/swift[0x134b32c]
/usr/bin/swift[0x1345616]
/usr/bin/swift[0xda0d8a]
/usr/bin/swift[0xda12e9]
/usr/bin/swift[0xda1947]
/usr/bin/swift[0x4c0570]
/usr/bin/swift[0x4bd7c9]
/usr/bin/swift[0x46e630]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7f8b1e1dab97]
/usr/bin/swift[0x46ca4a]
<unknown>:0: error: unable to execute command: Aborted
<unknown>:0: error: compile command failed due to signal 6 (use -v to see invocation)

Ответы [ 2 ]

1 голос
/ 20 июня 2020

Я запускаю его на игровой площадке и работает без проблем .. Попробуйте запустить его на Xcode. онлайн-компилятор может быть неисправен

Примечание: не используйте принудительное развертывание, используйте его с if let этим фрагментом кода

if let s = f2.sing?("Chirp") {
        print(s)
}

enter image description here

1 голос
/ 20 июня 2020

Похоже, вы используете онлайн-компилятор, который может быть неисправным или устаревшим. Обратитесь в службу поддержки и сообщите им об этой проблеме. Ваш код отлично работает на игровых площадках Xcode. Вы можете получить здесь (это Ma c или только для iPad).

...