Skip to content

Clarify the semantics of type cast #16963

Closed
@nodakai

Description

@nodakai

The current spec is not clear about such expressions as -1i32 as uint as i32 == -1i32 hold or not.

http://doc.rust-lang.org/rust.html#type-cast-expressions

7.2.11.5 Type cast expressions

A type cast expression is denoted with the binary operator as.

Executing an as expression casts the value on the left-hand side to the type on the right-hand side.

A numeric value can be cast to any numeric type. A raw pointer value can be cast to or from any integral type or raw pointer type. Any other cast is unsupported and will fail to compile.

An example of an as expression:

fn avg(v: &[f64]) -> f64 {
  let sum: f64 = sum(v);
  let sz: f64 = len(v) as f64;
  return sum / sz;
}

Cf: Java8 spec http://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.5

Background: I began to worry about this when I noticed some codes in libnative cast os::errno() into uint though POSIX defines errno to be a C int variable (on Linux, none of E*** actually have a negative value.)

http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html

The symbol errno shall expand to a modifiable lvalue of type int.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions