Skip to content

Commit 75ac2f6

Browse files
bors[bot]philberty
andauthored
Merge #1147
1147: Add missing coercion rule from array to slice r=philberty a=philberty Arrays are coercible into slices, this adds the missing type-resolution the rule which works for now. The other part of this fix is described in #1146 for coercion_site to be recursive and reuse the adjustment classes so that we reuse as much code as possible and handle complex coercion sites. Fixes #1129 Co-authored-by: Philip Herron <philip.herron@embecosm.com>
2 parents f0b6dca + 639b3d6 commit 75ac2f6

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

gcc/rust/typecheck/rust-tyty-coercion.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,22 @@ class SliceCoercionRules : public BaseCoercionRules
886886
TyVar (base_resolved->get_ref ()));
887887
}
888888

889+
void visit (ArrayType &type) override
890+
{
891+
// check base type
892+
auto base_resolved
893+
= base->get_element_type ()->unify (type.get_element_type ());
894+
if (base_resolved == nullptr)
895+
{
896+
BaseCoercionRules::visit (type);
897+
return;
898+
}
899+
900+
resolved = new SliceType (type.get_ref (), type.get_ty_ref (),
901+
type.get_ident ().locus,
902+
TyVar (base_resolved->get_ref ()));
903+
}
904+
889905
private:
890906
BaseType *get_base () override { return base; }
891907

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// { dg-additional-options "-w" }
2+
fn write_u8(i: u8) {
3+
let x: &[u8] = &[i];
4+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// { dg-additional-options "-w" }
2+
pub trait Hasher {
3+
fn finish(&self) -> u64;
4+
fn write(&mut self, bytes: &[u8]);
5+
fn write_u8(&mut self, i: u8) {
6+
self.write(&[i])
7+
}
8+
}
9+
10+
struct SipHasher;
11+
12+
impl Hasher for SipHasher {
13+
#[inline]
14+
fn write(&mut self, msg: &[u8]) {
15+
loop {}
16+
}
17+
18+
#[inline]
19+
fn finish(&self) -> u64 {
20+
0
21+
}
22+
}

0 commit comments

Comments
 (0)