Skip to content

Commit

Permalink
Use usual lifetime elision rules for foreign functions
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Aug 5, 2017
1 parent 5c7add7 commit 7704762
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
16 changes: 2 additions & 14 deletions src/librustc/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1081,20 +1081,8 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
Some(body)
}

// `fn(...) -> R` and `Trait(...) -> R` (both types and bounds).
hir::map::NodeTy(_) | hir::map::NodeTraitRef(_) => None,

// Foreign `fn` decls are terrible because we messed up,
// and their return types get argument type elision.
// And now too much code out there is abusing this rule.
hir::map::NodeForeignItem(_) => {
let arg_scope = Scope::Elision {
elide: arg_elide,
s: self.scope
};
self.with(arg_scope, |_, this| this.visit_ty(output));
return;
}
// Foreign functions, `fn(...) -> R` and `Trait(...) -> R` (both types and bounds).
hir::map::NodeForeignItem(_) | hir::map::NodeTy(_) | hir::map::NodeTraitRef(_) => None,

// Everything else (only closures?) doesn't
// actually enjoy elision in return types.
Expand Down
16 changes: 16 additions & 0 deletions src/test/compile-fail/foreign-fn-return-lifetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

extern "C" {
fn g(_: &u8) -> &u8; // OK
fn f() -> &u8; //~ ERROR missing lifetime specifier
}

fn main() {}

0 comments on commit 7704762

Please sign in to comment.