Skip to content

Commit a668dd2

Browse files
committed
Auto merge of #28827 - thepowersgang:unsafe-const-fn-2, r=Aatch
This is the original test implementation, which works according to the tests I wrote, but might need a review.
2 parents 294ef5b + 4c88bf2 commit a668dd2

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4380,11 +4380,11 @@ impl<'a> Parser<'a> {
43804380
/// - `extern fn`
43814381
/// - etc
43824382
pub fn parse_fn_front_matter(&mut self) -> PResult<(ast::Constness, ast::Unsafety, abi::Abi)> {
4383+
let unsafety = try!(self.parse_unsafety());
43834384
let is_const_fn = try!(self.eat_keyword(keywords::Const));
43844385
let (constness, unsafety, abi) = if is_const_fn {
4385-
(Constness::Const, Unsafety::Normal, abi::Rust)
4386+
(Constness::Const, unsafety, abi::Rust)
43864387
} else {
4387-
let unsafety = try!(self.parse_unsafety());
43884388
let abi = if try!(self.eat_keyword(keywords::Extern)) {
43894389
try!(self.parse_opt_abi()).unwrap_or(abi::C)
43904390
} else {
@@ -5378,9 +5378,14 @@ impl<'a> Parser<'a> {
53785378
} else {
53795379
abi::Rust
53805380
};
5381+
let constness = if abi == abi::Rust && try!(self.eat_keyword(keywords::Const) ){
5382+
Constness::Const
5383+
} else {
5384+
Constness::NotConst
5385+
};
53815386
try!(self.expect_keyword(keywords::Fn));
53825387
let (ident, item_, extra_attrs) =
5383-
try!(self.parse_item_fn(Unsafety::Unsafe, Constness::NotConst, abi));
5388+
try!(self.parse_item_fn(Unsafety::Unsafe, constness, abi));
53845389
let last_span = self.last_span;
53855390
let item = self.mk_item(lo,
53865391
last_span.hi,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2015 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+
// A quick test of 'unsafe const fn' functionality
12+
13+
#![feature(const_fn)]
14+
15+
unsafe const fn dummy(v: u32) -> u32 {
16+
!v
17+
}
18+
19+
const VAL: u32 = dummy(0xFFFF); //~ ERROR E0133
20+
21+
fn main() {
22+
assert_eq!(VAL, 0xFFFF0000);
23+
}
24+

src/test/run-pass/unsafe-const-fn.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2015 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+
// A quick test of 'unsafe const fn' functionality
12+
13+
#![feature(const_fn)]
14+
15+
unsafe const fn dummy(v: u32) -> u32 {
16+
!v
17+
}
18+
19+
struct Type;
20+
impl Type {
21+
unsafe const fn new() -> Type {
22+
Type
23+
}
24+
}
25+
26+
const VAL: u32 = unsafe { dummy(0xFFFF) };
27+
const TYPE_INST: Type = unsafe { Type::new() };
28+
29+
fn main() {
30+
assert_eq!(VAL, 0xFFFF0000);
31+
}

0 commit comments

Comments
 (0)