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

ices/83765.rs: fixed with errors #1039

Merged
merged 1 commit into from
Dec 6, 2021
Merged

ices/83765.rs: fixed with errors #1039

merged 1 commit into from
Dec 6, 2021

Conversation

github-actions[bot]
Copy link
Contributor

@github-actions github-actions bot commented Dec 6, 2021

Issue: rust-lang/rust#83765

#![feature(generic_const_exprs)]

trait TensorDimension {
    const DIM: usize;
    const ISSCALAR: bool = Self::DIM == 0;
    fn is_scalar(&self) -> bool {
        Self::ISSCALAR
    }
}

trait TensorSize: TensorDimension {
    fn size(&self) -> [usize; Self::DIM];
    fn inbounds(&self, index: [usize; Self::DIM]) -> bool {
        index.iter().zip(self.size().iter()).all(|(i, s)| i < s)
    }
}

trait Broadcastable: TensorSize + Sized {
    type Element;
    fn bget(&self, index: [usize; Self::DIM]) -> Option<Self::Element>;
    fn lazy_updim<const NEWDIM: usize>(
        &self,
        size: [usize; NEWDIM],
    ) -> LazyUpdim<Self, { Self::DIM }, NEWDIM> {
        assert!(
            NEWDIM >= Self::DIM,
            "Updimmed tensor cannot have fewer indices than the initial one."
        ); // const generic bounds on nightly. ( )
        LazyUpdim {
            size,
            reference: &self,
        }
    }
    fn bmap<T, F: Fn(Self::Element) -> T>(&self, foo: F) -> BMap<T, Self, F, { Self::DIM }> {
        BMap {
            reference: self,
            closure: foo,
        }
    }
}

struct LazyUpdim<'a, T: Broadcastable, const OLDDIM: usize, const DIM: usize> {
    size: [usize; DIM],
    reference: &'a T,
}

impl<'a, T: Broadcastable, const DIM: usize> TensorDimension for LazyUpdim<'a, T, { T::DIM }, DIM> {
    const DIM: usize = DIM;
}
impl<'a, T: Broadcastable, const DIM: usize> TensorSize for LazyUpdim<'a, T, { T::DIM }, DIM> {
    fn size(&self) -> [usize; DIM] {
        self.size
    }
}
impl<'a, T: Broadcastable, const DIM: usize> Broadcastable for LazyUpdim<'a, T, { T::DIM }, DIM> {
    type Element = T::Element;
    fn bget(&self, index: [usize; DIM]) -> Option<Self::Element> {
        assert!(DIM >= T::DIM);
        if !self.inbounds(index) {
            return None;
        }
        let size = self.size();
        let newindex: [usize; T::DIM] = Default::default(); //array_init::array_init(|i| if size[i] > 1 {index[i]} else {0});
        self.reference.bget(newindex)
    }
}

struct BMap<'a, R, T: Broadcastable, F: Fn(T::Element) -> R, const DIM: usize> {
    reference: &'a T,
    closure: F,
}

impl<'a, R, T: Broadcastable, F: Fn(T::Element) -> R, const DIM: usize> TensorDimension
    for BMap<'a, R, T, F, DIM>
{
    const DIM: usize = DIM;
}
impl<'a, R, T: Broadcastable, F: Fn(T::Element) -> R, const DIM: usize> TensorSize
    for BMap<'a, R, T, F, DIM>
{
    fn size(&self) -> [usize; DIM] {
        self.reference.size()
    }
}
impl<'a, R, T: Broadcastable, F: Fn(T::Element) -> R, const DIM: usize> Broadcastable
    for BMap<'a, R, T, F, DIM>
{
    type Element = R;
    fn bget(&self, index: [usize; DIM]) -> Option<Self::Element> {
        self.reference.bget(index).map(&self.closure)
    }
}

impl<T> TensorDimension for Vec<T> {
    const DIM: usize = 1;
}
impl<T> TensorSize for Vec<T> {
    fn size(&self) -> [usize; 1] {
        [self.len()]
    }
}
impl<T: Clone> Broadcastable for Vec<T> {
    type Element = T;
    fn bget(&self, index: [usize; 1]) -> Option<T> {
        self.get(index[0]).cloned()
    }
}

fn main() {
    let v = vec![1, 2, 3];
    let bv = v.lazy_updim([3, 4]);
    let bbv = bv.bmap(|x| x * x);

    println!(
        "The size of v is {:?}",
        bbv.bget([0, 2]).expect("Out of bounds.")
    );
}
=== stdout ===
=== stderr ===
warning: the feature `generic_const_exprs` is incomplete and may not be safe to use and/or cause compiler crashes
 --> /home/runner/work/glacier/glacier/ices/83765.rs:1:12
  |
1 | #![feature(generic_const_exprs)]
  |            ^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(incomplete_features)]` on by default
  = note: see issue #76560 <https://github.com/rust-lang/rust/issues/76560> for more information

error[E0308]: method not compatible with trait
  --> /home/runner/work/glacier/glacier/ices/83765.rs:51:5
   |
51 |     fn size(&self) -> [usize; DIM] {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Self::DIM`, found `DIM`
   |
   = note: expected type `Self::DIM`
              found type `DIM`

error[E0308]: method not compatible with trait
  --> /home/runner/work/glacier/glacier/ices/83765.rs:57:5
   |
57 |     fn bget(&self, index: [usize; DIM]) -> Option<Self::Element> {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Self::DIM`, found `DIM`
   |
   = note: expected type `Self::DIM`
              found type `DIM`

error[E0308]: method not compatible with trait
  --> /home/runner/work/glacier/glacier/ices/83765.rs:81:5
   |
81 |     fn size(&self) -> [usize; DIM] {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Self::DIM`, found `DIM`
   |
   = note: expected type `Self::DIM`
              found type `DIM`

error[E0308]: method not compatible with trait
  --> /home/runner/work/glacier/glacier/ices/83765.rs:89:5
   |
89 |     fn bget(&self, index: [usize; DIM]) -> Option<Self::Element> {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Self::DIM`, found `DIM`
   |
   = note: expected type `Self::DIM`
              found type `DIM`

error: unconstrained generic constant
  --> /home/runner/work/glacier/glacier/ices/83765.rs:59:18
   |
59 |         if !self.inbounds(index) {
   |                  ^^^^^^^^
   |
   = help: try adding a `where` bound using this expression: `where [(); Self::DIM]:`
note: required by a bound in `TensorSize::inbounds`
  --> /home/runner/work/glacier/glacier/ices/83765.rs:13:39
   |
13 |     fn inbounds(&self, index: [usize; Self::DIM]) -> bool {
   |                                       ^^^^^^^^^ required by this bound in `TensorSize::inbounds`

error[E0308]: mismatched types
  --> /home/runner/work/glacier/glacier/ices/83765.rs:59:27
   |
59 |         if !self.inbounds(index) {
   |                           ^^^^^ expected `Self::DIM`, found `DIM`
   |
   = note: expected type `Self::DIM`
              found type `DIM`

error: unconstrained generic constant
  --> /home/runner/work/glacier/glacier/ices/83765.rs:62:25
   |
62 |         let size = self.size();
   |                         ^^^^
   |
   = help: try adding a `where` bound using this expression: `where [(); Self::DIM]:`
note: required by a bound in `TensorSize::size`
  --> /home/runner/work/glacier/glacier/ices/83765.rs:12:31
   |
12 |     fn size(&self) -> [usize; Self::DIM];
   |                               ^^^^^^^^^ required by this bound in `TensorSize::size`

error[E0277]: the trait bound `[usize; _]: Default` is not satisfied
  --> /home/runner/work/glacier/glacier/ices/83765.rs:63:41
   |
63 |         let newindex: [usize; T::DIM] = Default::default(); //array_init::array_init(|i| if size[i] > 1 {index[i]} else {0});
   |                                         ^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `[usize; _]`
   |
help: consider introducing a `where` bound, but there might be an alternative better way to express this requirement
   |
55 | impl<'a, T: Broadcastable, const DIM: usize> Broadcastable for LazyUpdim<'a, T, { T::DIM }, DIM> where [usize; _]: Default {
   |                                                                                                  +++++++++++++++++++++++++

error: unconstrained generic constant
  --> /home/runner/work/glacier/glacier/ices/83765.rs:82:24
   |
82 |         self.reference.size()
   |                        ^^^^
   |
   = help: try adding a `where` bound using this expression: `where [(); Self::DIM]:`
note: required by a bound in `TensorSize::size`
  --> /home/runner/work/glacier/glacier/ices/83765.rs:12:31
   |
12 |     fn size(&self) -> [usize; Self::DIM];
   |                               ^^^^^^^^^ required by this bound in `TensorSize::size`

error[E0308]: mismatched types
  --> /home/runner/work/glacier/glacier/ices/83765.rs:82:9
   |
82 |         self.reference.size()
   |         ^^^^^^^^^^^^^^^^^^^^^ expected `DIM`, found `Self::DIM`
   |
   = note: expected type `DIM`
              found type `Self::DIM`

error: unconstrained generic constant
  --> /home/runner/work/glacier/glacier/ices/83765.rs:90:24
   |
90 |         self.reference.bget(index).map(&self.closure)
   |                        ^^^^
   |
   = help: try adding a `where` bound using this expression: `where [(); Self::DIM]:`
note: required by a bound in `Broadcastable::bget`
  --> /home/runner/work/glacier/glacier/ices/83765.rs:20:35
   |
20 |     fn bget(&self, index: [usize; Self::DIM]) -> Option<Self::Element>;
   |                                   ^^^^^^^^^ required by this bound in `Broadcastable::bget`

error[E0308]: mismatched types
  --> /home/runner/work/glacier/glacier/ices/83765.rs:90:29
   |
90 |         self.reference.bget(index).map(&self.closure)
   |                             ^^^^^ expected `Self::DIM`, found `DIM`
   |
   = note: expected type `Self::DIM`
              found type `DIM`

error: aborting due to 12 previous errors; 1 warning emitted

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
==============

=== stdout ===
=== stderr ===
warning: the feature `generic_const_exprs` is incomplete and may not be safe to use and/or cause compiler crashes
 --> /home/runner/work/glacier/glacier/ices/83765.rs:1:12
  |
1 | #![feature(generic_const_exprs)]
  |            ^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(incomplete_features)]` on by default
  = note: see issue #76560 <rust-lang/rust#76560> for more information

error[E0308]: method not compatible with trait
  --> /home/runner/work/glacier/glacier/ices/83765.rs:51:5
   |
51 |     fn size(&self) -> [usize; DIM] {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Self::DIM`, found `DIM`
   |
   = note: expected type `Self::DIM`
              found type `DIM`

error[E0308]: method not compatible with trait
  --> /home/runner/work/glacier/glacier/ices/83765.rs:57:5
   |
57 |     fn bget(&self, index: [usize; DIM]) -> Option<Self::Element> {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Self::DIM`, found `DIM`
   |
   = note: expected type `Self::DIM`
              found type `DIM`

error[E0308]: method not compatible with trait
  --> /home/runner/work/glacier/glacier/ices/83765.rs:81:5
   |
81 |     fn size(&self) -> [usize; DIM] {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Self::DIM`, found `DIM`
   |
   = note: expected type `Self::DIM`
              found type `DIM`

error[E0308]: method not compatible with trait
  --> /home/runner/work/glacier/glacier/ices/83765.rs:89:5
   |
89 |     fn bget(&self, index: [usize; DIM]) -> Option<Self::Element> {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Self::DIM`, found `DIM`
   |
   = note: expected type `Self::DIM`
              found type `DIM`

error: unconstrained generic constant
  --> /home/runner/work/glacier/glacier/ices/83765.rs:59:18
   |
59 |         if !self.inbounds(index) {
   |                  ^^^^^^^^
   |
   = help: try adding a `where` bound using this expression: `where [(); Self::DIM]:`
note: required by a bound in `TensorSize::inbounds`
  --> /home/runner/work/glacier/glacier/ices/83765.rs:13:39
   |
13 |     fn inbounds(&self, index: [usize; Self::DIM]) -> bool {
   |                                       ^^^^^^^^^ required by this bound in `TensorSize::inbounds`

error[E0308]: mismatched types
  --> /home/runner/work/glacier/glacier/ices/83765.rs:59:27
   |
59 |         if !self.inbounds(index) {
   |                           ^^^^^ expected `Self::DIM`, found `DIM`
   |
   = note: expected type `Self::DIM`
              found type `DIM`

error: unconstrained generic constant
  --> /home/runner/work/glacier/glacier/ices/83765.rs:62:25
   |
62 |         let size = self.size();
   |                         ^^^^
   |
   = help: try adding a `where` bound using this expression: `where [(); Self::DIM]:`
note: required by a bound in `TensorSize::size`
  --> /home/runner/work/glacier/glacier/ices/83765.rs:12:31
   |
12 |     fn size(&self) -> [usize; Self::DIM];
   |                               ^^^^^^^^^ required by this bound in `TensorSize::size`

error[E0277]: the trait bound `[usize; _]: Default` is not satisfied
  --> /home/runner/work/glacier/glacier/ices/83765.rs:63:41
   |
63 |         let newindex: [usize; T::DIM] = Default::default(); //array_init::array_init(|i| if size[i] > 1 {index[i]} else {0});
   |                                         ^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `[usize; _]`
   |
help: consider introducing a `where` bound, but there might be an alternative better way to express this requirement
   |
55 | impl<'a, T: Broadcastable, const DIM: usize> Broadcastable for LazyUpdim<'a, T, { T::DIM }, DIM> where [usize; _]: Default {
   |                                                                                                  +++++++++++++++++++++++++

error: unconstrained generic constant
  --> /home/runner/work/glacier/glacier/ices/83765.rs:82:24
   |
82 |         self.reference.size()
   |                        ^^^^
   |
   = help: try adding a `where` bound using this expression: `where [(); Self::DIM]:`
note: required by a bound in `TensorSize::size`
  --> /home/runner/work/glacier/glacier/ices/83765.rs:12:31
   |
12 |     fn size(&self) -> [usize; Self::DIM];
   |                               ^^^^^^^^^ required by this bound in `TensorSize::size`

error[E0308]: mismatched types
  --> /home/runner/work/glacier/glacier/ices/83765.rs:82:9
   |
82 |         self.reference.size()
   |         ^^^^^^^^^^^^^^^^^^^^^ expected `DIM`, found `Self::DIM`
   |
   = note: expected type `DIM`
              found type `Self::DIM`

error: unconstrained generic constant
  --> /home/runner/work/glacier/glacier/ices/83765.rs:90:24
   |
90 |         self.reference.bget(index).map(&self.closure)
   |                        ^^^^
   |
   = help: try adding a `where` bound using this expression: `where [(); Self::DIM]:`
note: required by a bound in `Broadcastable::bget`
  --> /home/runner/work/glacier/glacier/ices/83765.rs:20:35
   |
20 |     fn bget(&self, index: [usize; Self::DIM]) -> Option<Self::Element>;
   |                                   ^^^^^^^^^ required by this bound in `Broadcastable::bget`

error[E0308]: mismatched types
  --> /home/runner/work/glacier/glacier/ices/83765.rs:90:29
   |
90 |         self.reference.bget(index).map(&self.closure)
   |                             ^^^^^ expected `Self::DIM`, found `DIM`
   |
   = note: expected type `Self::DIM`
              found type `DIM`

error: aborting due to 12 previous errors; 1 warning emitted

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
==============
@Alexendoo Alexendoo merged commit ac8fefe into master Dec 6, 2021
@Alexendoo Alexendoo deleted the autofix/ices/83765.rs branch December 6, 2021 13:42
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