Skip to content

New compiler error in beta: this constant cannot be used, attempted to read undefined bytes #54387

Closed
@sdroege

Description

@sdroege

Since latest beta (and nightly), some of my crates have errors like the following

error: this constant cannot be used
  --> src/udpsrc.rs:47:1
   |
47 | const DEFAULT_CAPS: Option<gst::Caps> = None;
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempted to read undefined bytes

A standalone testcase replicating all the involved types can be found below (depends only on the libc crate).

I don't know if the error is valid, but in any case I don't understand what it means so at least the error message could be improved a bit :)

use std::marker;
use std::ptr;

extern crate libc;
use libc::{c_int, c_uint, c_void, size_t};

#[repr(C)]
pub struct CapsRef(ffi::GstCaps);

pub type Caps = GstRc<CapsRef>;

pub trait MiniObject {}
impl MiniObject for CapsRef {}

pub struct GstRc<T: MiniObject> {
    obj: ptr::NonNull<T>,
    borrowed: bool,
    phantom: marker::PhantomData<T>,
}

mod ffi {
    use super::*;

    #[repr(C)]
    #[derive(Copy, Clone)]
    pub struct GstCaps {
        pub mini_object: GstMiniObject,
    }

    #[repr(C)]
    #[derive(Copy, Clone)]
    pub struct GstMiniObject {
        pub type_: GType,
        pub refcount: c_int,
        pub lockstate: c_int,
        pub flags: c_uint,
        pub copy: GstMiniObjectCopyFunction,
        pub dispose: GstMiniObjectDisposeFunction,
        pub free: GstMiniObjectFreeFunction,
        pub n_qdata: c_uint,
        pub qdata: gpointer,
    }

    pub type GstMiniObjectCopyFunction =
        Option<unsafe extern "C" fn(*const GstMiniObject) -> *mut GstMiniObject>;
    pub type GstMiniObjectDisposeFunction =
        Option<unsafe extern "C" fn(*mut GstMiniObject) -> gboolean>;
    pub type GstMiniObjectFreeFunction = Option<unsafe extern "C" fn(*mut GstMiniObject)>;

    pub type gboolean = c_int;

    pub type GType = size_t;

    pub type gpointer = *mut c_void;

}

const FOO: Option<Caps> = None;

fn main() {
    let _meh = FOO;
}

Metadata

Metadata

Assignees

Labels

A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)P-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-betaPerformance or correctness regression from stable to beta.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions