Skip to content

Fix the ordering of unsafe and extern in methods #19418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1296,13 +1296,14 @@ impl<'a> Parser<'a> {
let lo = p.span.lo;

let vis = p.parse_visibility();
let style = p.parse_fn_style();
let abi = if p.eat_keyword(keywords::Extern) {
p.parse_opt_abi().unwrap_or(abi::C)
} else {
abi::Rust
};
p.expect_keyword(keywords::Fn);

let style = p.parse_fn_style();
let ident = p.parse_ident();
let mut generics = p.parse_generics();

Expand Down Expand Up @@ -4458,12 +4459,13 @@ impl<'a> Parser<'a> {
self.span.hi) };
(ast::MethMac(m), self.span.hi, attrs)
} else {
let fn_style = self.parse_fn_style();
let abi = if self.eat_keyword(keywords::Extern) {
self.parse_opt_abi().unwrap_or(abi::C)
} else {
abi::Rust
};
let fn_style = self.parse_fn_style();
self.expect_keyword(keywords::Fn);
let ident = self.parse_ident();
let mut generics = self.parse_generics();
let (explicit_self, decl) = self.parse_fn_decl_with_self(|p| {
Expand Down Expand Up @@ -5009,14 +5011,13 @@ impl<'a> Parser<'a> {
})
}

/// Parse safe/unsafe and fn
/// Parse unsafe or not
fn parse_fn_style(&mut self) -> FnStyle {
if self.eat_keyword(keywords::Fn) { NormalFn }
else if self.eat_keyword(keywords::Unsafe) {
self.expect_keyword(keywords::Fn);
if self.eat_keyword(keywords::Unsafe) {
UnsafeFn
} else {
NormalFn
}
else { self.unexpected(); }
}


Expand Down
15 changes: 15 additions & 0 deletions src/test/compile-fail/issue-19398.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2014 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.

trait T {
extern "Rust" unsafe fn foo(); //~ ERROR expected `fn`, found `unsafe`
}

fn main() {}
2 changes: 1 addition & 1 deletion src/test/compile-fail/removed-syntax-static-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
struct S;

impl S {
static fn f() {} //~ ERROR unexpected token: `static`
static fn f() {} //~ ERROR expected `fn`, found `static`
}
19 changes: 19 additions & 0 deletions src/test/run-pass/issue-19398.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2014 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.

trait T {
unsafe extern "Rust" fn foo();
}

impl T for () {
unsafe extern "Rust" fn foo() {}
}

fn main() {}