Skip to content

Commit

Permalink
Allow emission of i64::MIN integer literal (rust-lang#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
jethrogb authored and Yamakaky committed Jul 17, 2016
1 parent 0424778 commit 88f4ee0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Changed
- Convert `float` and `double` to `f32` and `f64` by default (#348).

### Fixed
- Allow emission of -9223372036854775808 integer literal (#375)

## [0.18.0] - 2016-06-05
### Breaking
- New command line interface.
Expand Down
10 changes: 9 additions & 1 deletion src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -954,9 +954,17 @@ fn cunion_to_rs(ctx: &mut GenCtx,
items
}

fn i64_abs(i: i64) -> u64 {
if i<0 {
i.wrapping_neg() as u64
} else {
i as u64
}
}

/// Converts a signed number to AST Expression.
fn i64_to_int_lit(ctx: &mut GenCtx, value: i64) -> P<ast::Expr> {
let int_lit = ast::LitKind::Int(value.abs() as u64, ast::LitIntType::Unsuffixed);
let int_lit = ast::LitKind::Int(i64_abs(value), ast::LitIntType::Unsuffixed);
let expr = ctx.ext_cx.expr_lit(ctx.span, int_lit);
if value < 0 {
let negated = ast::ExprKind::Unary(ast::UnOp::Neg, expr);
Expand Down
4 changes: 4 additions & 0 deletions tests/headers/enum_explicit_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ enum MuchLong: long {
enum MuchLongLong: unsigned long long {
MuchHigh = 4294967296,
};

enum MostLongLong: long long {
MostLow = -9223372036854775808,
};
6 changes: 6 additions & 0 deletions tests/test_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ fn with_explicitly_typed_cxx_enum() {
#[repr(u64)]
#[derive(Debug)]
pub enum MuchLongLong { MuchHigh = 4294967296, }
#[derive(Copy, Clone)]
#[repr(i64)]
#[derive(Debug)]
pub enum MostLongLong { MostLow = -9223372036854775808, }
");
assert_bind_eq(default_without_rust_enums(), "headers/enum_explicit_type.hpp", "
pub type Foo = u8;
Expand All @@ -110,6 +114,8 @@ fn with_explicitly_typed_cxx_enum() {
pub const MuchLow: MuchLong = -4294967296;
pub type MuchLongLong = u64;
pub const MuchHigh: MuchLongLong = 4294967296;
pub type MostLongLong = i64;
pub const MostLow: MostLongLong = -9223372036854775808;
");
}

Expand Down

0 comments on commit 88f4ee0

Please sign in to comment.