Skip to content
This repository was archived by the owner on May 23, 2024. It is now read-only.

Conversation

@github-actions
Copy link
Contributor

Issue: rust-lang/rust#62456

#![feature(const_generics)]

use std::fmt;

struct Builder<const N: usize> {
    items: [&'static str; N],
}

fn new_builder() -> Builder<{0}> {
    return Builder{items: []};
}

impl<const N: usize> Builder<{ N }> {
    fn append(self, value: &'static str) -> Builder<{ N + 1 }> {
        let mut new_items = [""; N + 1];
        new_items[..N].copy_from_slice(self.items);
        new_items[N] = value;
        return Builder { items: new_items };
    }

    fn build(self) -> Final<{ N }> {
        return Final { items: self.items };
    }
}

struct Final<const N: usize> {
    items: [&'static str; N],
}

impl<const N: usize> fmt::Debug for Final<{ N }> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Final")
            .field("items", &&self.items[..])
            .finish()
    }
}

fn main() {
    let f = new_builder()
        .append("abc")
        .append("def")
        .append("ghi")
        .build();
    println!("f={:?}", f);
}
=== stdout ===
=== stderr ===
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
 --> /home/runner/work/glacier/glacier/ices/62456.rs:1:12
  |
1 | #![feature(const_generics)]
  |            ^^^^^^^^^^^^^^
  |
  = note: `#[warn(incomplete_features)]` on by default

error: array lengths can't depend on generic parameters
  --> /home/runner/work/glacier/glacier/ices/62456.rs:15:34
   |
15 |         let mut new_items = [""; N + 1];
   |                                  ^^^^^

error: aborting due to previous error

==============

=== stdout ===
=== stderr ===
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
 --> /home/runner/work/glacier/glacier/ices/62456.rs:1:12
  |
1 | #![feature(const_generics)]
  |            ^^^^^^^^^^^^^^
  |
  = note: `#[warn(incomplete_features)]` on by default

error: array lengths can't depend on generic parameters
  --> /home/runner/work/glacier/glacier/ices/62456.rs:15:34
   |
15 |         let mut new_items = [""; N + 1];
   |                                  ^^^^^

error: aborting due to previous error

==============
@JohnTitor JohnTitor merged commit 6d8386b into master Mar 12, 2020
@JohnTitor JohnTitor deleted the autofix/ices/62456.rs branch March 12, 2020 12:10
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants