Skip to content
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

lazy_type_alias: error doesn't have a size known at compile-time #114214

Closed
matthiaskrgr opened this issue Jul 29, 2023 · 4 comments
Closed

lazy_type_alias: error doesn't have a size known at compile-time #114214

matthiaskrgr opened this issue Jul 29, 2023 · 4 comments
Labels
C-bug Category: This is a bug. E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example F-lazy_type_alias `#![feature(lazy_type_alias)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

I tried this code:
src/tools/miri/tests/pass/issues/issue-36278-prefix-nesting.rs

// Issue 36278: On an unsized struct with >1 level of nontrivial
// nesting, ensure we are computing dynamic size of prefix correctly.

use std::mem;

const SZ: usize = 100;
struct P<T: ?Sized>([u8; SZ], T);

type Ack<T> = P<P<T>>;

fn main() {
    let size_of_sized;
    let size_of_unsized;
    let x: Box<Ack<[u8; 0]>> = Box::new(P([0; SZ], P([0; SZ], [0; 0])));
    size_of_sized = mem::size_of_val::<Ack<_>>(&x);
    let align_of_sized = mem::align_of_val::<Ack<_>>(&x);
    let y: Box<Ack<[u8]>> = x;
    size_of_unsized = mem::size_of_val::<Ack<_>>(&y);
    assert_eq!(size_of_sized, size_of_unsized);
    assert_eq!(align_of_sized, 1);
    assert_eq!(mem::align_of_val::<Ack<_>>(&y), 1);
}

without `-Zcrate-attr=feature(lazy_type_alias)´: dead code warnings

with -Zcrate-attr=feature(lazy_type_alias):

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
  --> src/tools/miri/tests/pass/issues/issue-36278-prefix-nesting.rs:17:16
   |
17 |     let y: Box<Ack<[u8]>> = x;
   |                ^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `Sized` is not implemented for `[u8]`
note: required by a bound on the type alias `Ack`
  --> src/tools/miri/tests/pass/issues/issue-36278-prefix-nesting.rs:9:10
   |
9  | type Ack<T> = P<P<T>>;
   |          ^ required by this bound

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
  --> src/tools/miri/tests/pass/issues/issue-36278-prefix-nesting.rs:18:42
   |
18 |     size_of_unsized = mem::size_of_val::<Ack<_>>(&y);
   |                                          ^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `Sized` is not implemented for `[u8]`
note: required by a bound on the type alias `Ack`
  --> src/tools/miri/tests/pass/issues/issue-36278-prefix-nesting.rs:9:10
   |
9  | type Ack<T> = P<P<T>>;
   |          ^ required by this bound

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
  --> src/tools/miri/tests/pass/issues/issue-36278-prefix-nesting.rs:21:36
   |
21 |     assert_eq!(mem::align_of_val::<Ack<_>>(&y), 1);
   |                                    ^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `Sized` is not implemented for `[u8]`
note: required by a bound on the type alias `Ack`
  --> src/tools/miri/tests/pass/issues/issue-36278-prefix-nesting.rs:9:10
   |
9  | type Ack<T> = P<P<T>>;
   |          ^ required by this bound

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0277`.
rustc 1.73.0-nightly (04abc370b 2023-07-28)
binary: rustc
commit-hash: 04abc370b9f3855b28172b65a7f7d5a433f41412
commit-date: 2023-07-28
host: x86_64-unknown-linux-gnu
release: 1.73.0-nightly
LLVM version: 16.0.5
@matthiaskrgr matthiaskrgr added C-bug Category: This is a bug. F-lazy_type_alias `#![feature(lazy_type_alias)]` labels Jul 29, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 29, 2023
@compiler-errors compiler-errors added E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jul 29, 2023
@matthiaskrgr
Copy link
Member Author

#![feature(lazy_type_alias)]

use std::mem;

const SZ: usize = 100;
struct P<T: ?Sized>([u8; SZ], T);

type Ack<T> = P<P<T>>;

fn main() {
    let size_of_sized;
    let size_of_unsized;
    let x: Box<Ack<[u8; 0]>> = Box::new(P([0; SZ], P([0; SZ], [0; 0])));
    size_of_sized = mem::size_of_val::<Ack<_>>(&x);

    let y: Box<Ack<[u8]>> = x;
    size_of_unsized = mem::size_of_val::<Ack<_>>(&y);
}

@fmease
Copy link
Member

fmease commented Jul 29, 2023

This one is not a bug ;)
Under lazy_type_alias we now check the generic parameter bounds & where-clauses on type aliases. In the code above, we check if T: Sized at each usage-site of Ack which is not the case for Ack<_> (i.e. Ack<[u8]>) e.g. in the second to last line, hence the error(s). You could make the code above compile by relaxing T: type Ack<T: ?Sized> = P<P<T>>;.

This is the defining feature of lazy_type_alias and a breaking change which is why it would need to be part of a separate edition.

@matthiaskrgr
Copy link
Member Author

😄 makes sense
Oli told me I could fuzz for compiles --lazy-type-alias--> compile fail cases but hat was also 2 weeks ago and I hadn't gotten to coding out that pattern until now 😅

@compiler-errors
Copy link
Member

Gonna close this since it's not a bug.

@compiler-errors compiler-errors closed this as not planned Won't fix, can't repro, duplicate, stale Jul 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example F-lazy_type_alias `#![feature(lazy_type_alias)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants