Skip to content
New issue

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

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

Already on GitHub? # to your account

Structure mismatch in "Generics - Phantom type parameters" example #560

Closed
adpaco-aws opened this issue Oct 18, 2021 · 2 comments · Fixed by #2406
Closed

Structure mismatch in "Generics - Phantom type parameters" example #560

adpaco-aws opened this issue Oct 18, 2021 · 2 comments · Fixed by #2406
Assignees
Labels
[C] Bug This is a bug. Something isn't working. [F] Crash Kani crashed

Comments

@adpaco-aws
Copy link
Contributor

The example in Rust by Example/Generics/Phantom type parameters/Testcase: unit clarification/24.rs, which can be accessed from https://doc.rust-lang.org/rust-by-example/generics/phantom/testcase_units.html fails to codegen with

thread 'rustc' panicked at 'assertion failed: `(left == right)`
left: `2`,
right: `1`: Error in struct_expr; mismatch in number of fields and values.
StructTag("tag-_14457343961392166270")
[Expr { value: DoubleConstant(12.0), typ: Double, location: None }]', compiler/cbmc/src/goto_program/expr.rs:703:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

error: internal compiler error: unexpected panic
@zhassan-aws
Copy link
Contributor

Crash is reproducible with af11d38d93a. This is the example for the reference:

use std::ops::Add;
use std::marker::PhantomData;

/// Create void enumerations to define unit types.
#[derive(Debug, Clone, Copy)]
enum Inch {}
#[derive(Debug, Clone, Copy)]
enum Mm {}

/// `Length` is a type with phantom type parameter `Unit`,
/// and is not generic over the length type (that is `f64`).
///
/// `f64` already implements the `Clone` and `Copy` traits.
#[derive(Debug, Clone, Copy)]
struct Length<Unit>(f64, PhantomData<Unit>);

/// The `Add` trait defines the behavior of the `+` operator.
impl<Unit> Add for Length<Unit> {
     type Output = Length<Unit>;

    // add() returns a new `Length` struct containing the sum.
    fn add(self, rhs: Length<Unit>) -> Length<Unit> {
        // `+` calls the `Add` implementation for `f64`.
        Length(self.0 + rhs.0, PhantomData)
    }
}

fn main() {
    // Specifies `one_foot` to have phantom type parameter `Inch`.
    let one_foot:  Length<Inch> = Length(12.0, PhantomData);
    // `one_meter` has phantom type parameter `Mm`.
    let one_meter: Length<Mm>   = Length(1000.0, PhantomData);

    // `+` calls the `add()` method we implemented for `Length<Unit>`.
    //
    // Since `Length` implements `Copy`, `add()` does not consume
    // `one_foot` and `one_meter` but copies them into `self` and `rhs`.
    let two_feet = one_foot + one_foot;
    let two_meters = one_meter + one_meter;

    // Addition works.
    println!("one foot + one_foot = {:?} in", two_feet.0);
    println!("one meter + one_meter = {:?} mm", two_meters.0);

    // Nonsensical operations fail as they should:
    // Compile-time Error: type mismatch.
    //let one_feter = one_foot + one_meter;
}

@zhassan-aws zhassan-aws added [C] Bug This is a bug. Something isn't working. MLP - Must Have labels Mar 17, 2022
celinval added a commit that referenced this issue Mar 22, 2022
tedinski pushed a commit to tedinski/rmc that referenced this issue Apr 22, 2022
tedinski pushed a commit to tedinski/rmc that referenced this issue Apr 25, 2022
tedinski pushed a commit to tedinski/rmc that referenced this issue Apr 26, 2022
tedinski pushed a commit that referenced this issue Apr 27, 2022
@celinval celinval added [F] Crash Kani crashed and removed Area: build [F] Crash Kani crashed labels Nov 9, 2022
@celinval
Copy link
Contributor

Still reproducible with Kani 0.15.0.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
[C] Bug This is a bug. Something isn't working. [F] Crash Kani crashed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants