From 9211c7c0d484e2ebe9435d7b1f2510c43644d1e5 Mon Sep 17 00:00:00 2001 From: gregomni Date: Thu, 10 Dec 2015 23:26:18 -0800 Subject: [PATCH] Better diagnose error coming from unwrapping optional before initialized. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added a check for whether the escape use is due to an UncheckedTakeEnumDataAddrInst, which accesses the memory to grab the .Some part of an Optional during “?.” or “!”. In that case, use the generic used-before-initialized diagnostic, since it isn’t actually because of closure capture. Added test cases, including specifically testing that capturing in a closure in order to unwrap the optional is still ok. --- lib/SILPasses/EarlySIL/DefiniteInitialization.cpp | 2 ++ test/SILPasses/definite_init_diagnostics.swift | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/SILPasses/EarlySIL/DefiniteInitialization.cpp b/lib/SILPasses/EarlySIL/DefiniteInitialization.cpp index f7e3dcd726ec2..8f8af66121c17 100644 --- a/lib/SILPasses/EarlySIL/DefiniteInitialization.cpp +++ b/lib/SILPasses/EarlySIL/DefiniteInitialization.cpp @@ -1094,6 +1094,8 @@ void LifetimeChecker::handleEscapeUse(const DIMemoryUse &Use) { DiagMessage = diag::variable_closure_use_uninit; else DiagMessage = diag::variable_function_use_uninit; + } else if (isa(Inst)) { + DiagMessage = diag::variable_used_before_initialized; } else { DiagMessage = diag::variable_closure_use_uninit; } diff --git a/test/SILPasses/definite_init_diagnostics.swift b/test/SILPasses/definite_init_diagnostics.swift index ad03ad2515952..96703d07ed66a 100644 --- a/test/SILPasses/definite_init_diagnostics.swift +++ b/test/SILPasses/definite_init_diagnostics.swift @@ -78,6 +78,12 @@ func test2() { takes_closure { // ok. markUsed(b3) } + + var b4 : Int? + takes_closure { // ok. + markUsed(b4!) + } + b4 = 7 // Structs var s1 : SomeStruct @@ -1184,3 +1190,9 @@ func test22436880() { x = 1 bug22436880(&x) // expected-error {{immutable value 'x' may not be passed inout}} } + +// sr-184 +let x: String? // expected-note 2 {{constant defined here}} +print(x?.characters.count) // expected-error {{constant 'x' used before being initialized}} +print(x!) // expected-error {{constant 'x' used before being initialized}} +