Skip to content

"error: target type not found for const cast" triggered by the position of a mod item or by adding #[deriving(Show)] #18128

New issue

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

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

Already on GitHub? Sign in to your account

Closed
japaric opened this issue Oct 17, 2014 · 2 comments
Labels
A-type-system Area: Type system

Comments

@japaric
Copy link
Member

japaric commented Oct 17, 2014

STR

// main.rs
extern crate libc;

#[cfg(error)]
mod trigger;

const CSTOPB_: u32 = 64;
const CREAD_: u32 = 128;

const CSTOPB: u32 = CSTOPB_;
// This cast to `c_uint` is necessary
const CREAD: u32 = CREAD_ as ::libc::c_uint;

#[cfg(not(error))]
mod trigger;

fn main() {}
// trigger.rs
#[repr(u32)]
enum Bit {
    CSTOPB = super::CSTOPB,
    CREAD = super::CREAD,
}

Output

rustc --cfg error main.rs generates the following compiler error:

main.rs:12:30: 12:44 error: target type not found for const cast
main.rs:12 const CREAD: u32 = CREAD_ as ::libc::c_uint;

rustc main.rs compiles successfully.

Version

rustc 0.13.0-dev (93e589c87 2014-10-17 09:22:14 +0000)

I can trace this behavior back to 0.11


I thought that the position of the items in a crate (except for use imports and extern crates which must go at the beginning) didn't affect the compiler output. That makes me think this is a bug.

@japaric
Copy link
Member Author

japaric commented Oct 17, 2014

Found another way to generate the same error, this time only by adding a #[deriving(Show)]

STR

extern crate libc;

pub mod raw {
    pub use self::ffi::tcflag_t;

    #[allow(dead_code, non_camel_case_types)]
    mod ffi {
        pub type tcflag_t = ::libc::c_uint;

        pub const CSTOPB_: ::libc::c_uint = 64;
        pub const CREAD_: ::libc::c_uint = 128;
    }

    pub const CSTOPB: tcflag_t = self::ffi::CSTOPB_ as tcflag_t;
    pub const CREAD: tcflag_t = self::ffi::CREAD_ as tcflag_t;
}

pub mod control {
    use raw;

    #[cfg(error)]
    #[repr(u32)]
    #[deriving(Show)]
    pub enum Bit {
        CSTOPB = raw::CSTOPB,
        CREAD = raw::CREAD,
    }

    #[cfg(not(error))]
    #[repr(u32)]
    pub enum Bit {
        CSTOPB = raw::CSTOPB,
        CREAD = raw::CREAD,
    }
}

fn main() {}

Output

main.rs:14:56: 14:64 error: target type not found for const cast
main.rs:14     pub const CSTOPB: tcflag_t = self::ffi::CSTOPB_ as tcflag_t;

Version

rustc 0.13.0-dev (93e589c87 2014-10-17 09:22:14 +0000)

0.12 generates the same error, but 0.11 compiles successfully


It seems there's some problem around the intersection of const and casting.

cc @alexcrichton

@japaric japaric changed the title "error: target type not found for const cast" triggered by the position of a mod item "error: target type not found for const cast" triggered by the position of a mod item or by adding #[deriving(Show)] Oct 17, 2014
@steveklabnik steveklabnik added the A-type-system Area: Type system label Jan 27, 2015
@steveklabnik
Copy link
Member

Both of these examples compile for me today.

lnicola pushed a commit to lnicola/rust that referenced this issue Sep 25, 2024
… r=Veykril

fix: Handle errors and lints from external macros

Some lints should not be reported if they originate from an external macro, and quickfixes should be disabled (or they'll change library code).

Fixes rust-lang#18122.
Closes rust-lang#18124.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

2 participants