Skip to content

Are generics under #![no_std] implicitly DST now?  #15788

Closed
@pnkfelix

Description

@pnkfelix

Transcript of something that I saw today that I thought was odd:

% cat /tmp/foo_no_std.rs
#![no_std]
#![crate_type="lib"]

pub fn foo<T>(b: bool, x: T, f: |T| -> int) -> int {
    if b {
        f(x)
    } else {
        3
    }
}
% cat /tmp/foo_w_std.rs
#![crate_type="lib"]

pub fn foo<T>(b: bool, x: T, f: |T| -> int) -> int {
    if b {
        f(x)
    } else {
        3
    }
}
% ~/opt/rust-0.10/bin/rustc --version
/Users/fklock/opt/rust-0.10/bin/rustc 0.10 (46867cc 2014-04-02 16:59:39 -0700)
host: x86_64-apple-darwin
% ~/opt/rust-0.11/bin/rustc --version
rustc 0.11.0 (aa1163b92de7717eb7c5eba002b4012e0574a7fe 2014-06-27 12:50:16 -0700)
% ~/opt/rust-0.10/bin/rustc /tmp/foo_w_std.rs
% ~/opt/rust-0.10/bin/rustc /tmp/foo_no_std.rs
% ~/opt/rust-0.11/bin/rustc /tmp/foo_w_std.rs
% ~/opt/rust-0.11/bin/rustc /tmp/foo_no_std.rs
/tmp/foo_no_std.rs:4:24: 4:25 error: variable `x` has dynamically sized type `T`
/tmp/foo_no_std.rs:4 pub fn foo<T>(b: bool, x: T, f: |T| -> int) -> int {
                                            ^
error: aborting due to previous error
% 

Now, to be fair, I do remember that I had to start adding the trait Sized with the appropriate lang item when using #![no_std] at some point. And that was okay, because I got a message telling me to do so (or at least, a message saying it was missing, which is close enough).

But this seems to go a step further and somehow remove the implicit Sized-ness from generic type parameters? (Or have I overlooked some obvious goof in the code above?)


At @cmr's suggestion, I changed the code to this:

#![feature(lang_items)]
#![no_std]
#![crate_type="lib"]

#[lang="sized"] pub trait Sized { }

pub fn foo<T>(b: bool, x: T, f: |T| -> int) -> int {
    if b {
        f(x)
    } else {
        3
    }
}

And it now compiles.

So okay -- maybe we just need to improve the error reporting for the original case to check if the Sized lang item is defined, and if not, include a note to the user that they may want to add it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-DSTsArea: Dynamically-sized types (DSTs)A-type-systemArea: Type system

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions