From 88599780e0eba38d9e543b7809f586479f6956bd Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Tue, 12 Feb 2019 14:53:49 +0100 Subject: accept `init: fn() -> init::LateResources` --- macros/src/check.rs | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) (limited to 'macros/src/check.rs') diff --git a/macros/src/check.rs b/macros/src/check.rs index 0bc31c5..045d152 100644 --- a/macros/src/check.rs +++ b/macros/src/check.rs @@ -35,17 +35,20 @@ pub fn app(app: &App) -> parse::Result<()> { } } - // Check that all late resources have been initialized in `#[init]` - for res in app - .resources - .iter() - .filter_map(|(name, res)| if res.expr.is_none() { Some(name) } else { None }) - { - if app.init.assigns.iter().all(|assign| assign.left != *res) { - return Err(parse::Error::new( - res.span(), - "late resources MUST be initialized at the end of `init`", - )); + // Check that all late resources have been initialized in `#[init]` if `init` has signature + // `fn()` + if !app.init.returns_late_resources { + for res in app + .resources + .iter() + .filter_map(|(name, res)| if res.expr.is_none() { Some(name) } else { None }) + { + if app.init.assigns.iter().all(|assign| assign.left != *res) { + return Err(parse::Error::new( + res.span(), + "late resources MUST be initialized at the end of `init`", + )); + } } } @@ -112,11 +115,19 @@ pub fn app(app: &App) -> parse::Result<()> { } } - // Check that `init` contains no early returns *if* late resources exist + // Check that `init` contains no early returns *if* late resources exist and `init` signature is + // `fn()` if app.resources.values().any(|res| res.expr.is_none()) { - for stmt in &app.init.stmts { - noreturn_stmt(stmt)?; + if !app.init.returns_late_resources { + for stmt in &app.init.stmts { + noreturn_stmt(stmt)?; + } } + } else if app.init.returns_late_resources { + return Err(parse::Error::new( + Span::call_site(), + "`init` signature must be `[unsafe] fn()` if there are no late resources", + )); } Ok(()) -- cgit v1.2.3