Skip to content

Commit 222a1eb

Browse files
committed
auto merge of #19418 : P1start/rust/unsafe-extern-trait, r=alexcrichton
Fixes #19398.
2 parents acad03a + 63553a1 commit 222a1eb

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,13 +1296,14 @@ impl<'a> Parser<'a> {
12961296
let lo = p.span.lo;
12971297

12981298
let vis = p.parse_visibility();
1299+
let style = p.parse_fn_style();
12991300
let abi = if p.eat_keyword(keywords::Extern) {
13001301
p.parse_opt_abi().unwrap_or(abi::C)
13011302
} else {
13021303
abi::Rust
13031304
};
1305+
p.expect_keyword(keywords::Fn);
13041306

1305-
let style = p.parse_fn_style();
13061307
let ident = p.parse_ident();
13071308
let mut generics = p.parse_generics();
13081309

@@ -4458,12 +4459,13 @@ impl<'a> Parser<'a> {
44584459
self.span.hi) };
44594460
(ast::MethMac(m), self.span.hi, attrs)
44604461
} else {
4462+
let fn_style = self.parse_fn_style();
44614463
let abi = if self.eat_keyword(keywords::Extern) {
44624464
self.parse_opt_abi().unwrap_or(abi::C)
44634465
} else {
44644466
abi::Rust
44654467
};
4466-
let fn_style = self.parse_fn_style();
4468+
self.expect_keyword(keywords::Fn);
44674469
let ident = self.parse_ident();
44684470
let mut generics = self.parse_generics();
44694471
let (explicit_self, decl) = self.parse_fn_decl_with_self(|p| {
@@ -5009,14 +5011,13 @@ impl<'a> Parser<'a> {
50095011
})
50105012
}
50115013

5012-
/// Parse safe/unsafe and fn
5014+
/// Parse unsafe or not
50135015
fn parse_fn_style(&mut self) -> FnStyle {
5014-
if self.eat_keyword(keywords::Fn) { NormalFn }
5015-
else if self.eat_keyword(keywords::Unsafe) {
5016-
self.expect_keyword(keywords::Fn);
5016+
if self.eat_keyword(keywords::Unsafe) {
50175017
UnsafeFn
5018+
} else {
5019+
NormalFn
50185020
}
5019-
else { self.unexpected(); }
50205021
}
50215022

50225023

src/test/compile-fail/issue-19398.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
trait T {
12+
extern "Rust" unsafe fn foo(); //~ ERROR expected `fn`, found `unsafe`
13+
}
14+
15+
fn main() {}

src/test/compile-fail/removed-syntax-static-fn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
struct S;
1212

1313
impl S {
14-
static fn f() {} //~ ERROR unexpected token: `static`
14+
static fn f() {} //~ ERROR expected `fn`, found `static`
1515
}

src/test/run-pass/issue-19398.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
trait T {
12+
unsafe extern "Rust" fn foo();
13+
}
14+
15+
impl T for () {
16+
unsafe extern "Rust" fn foo() {}
17+
}
18+
19+
fn main() {}

0 commit comments

Comments
 (0)