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>
This commit is contained in:
bors[bot] 2022-04-22 10:04:28 +00:00 committed by GitHub
commit 75ac2f6fc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 0 deletions

View File

@ -886,6 +886,22 @@ public:
TyVar (base_resolved->get_ref ()));
}
void visit (ArrayType &type) override
{
// check base type
auto base_resolved
= base->get_element_type ()->unify (type.get_element_type ());
if (base_resolved == nullptr)
{
BaseCoercionRules::visit (type);
return;
}
resolved = new SliceType (type.get_ref (), type.get_ty_ref (),
type.get_ident ().locus,
TyVar (base_resolved->get_ref ()));
}
private:
BaseType *get_base () override { return base; }

View File

@ -0,0 +1,4 @@
// { dg-additional-options "-w" }
fn write_u8(i: u8) {
let x: &[u8] = &[i];
}

View File

@ -0,0 +1,22 @@
// { dg-additional-options "-w" }
pub trait Hasher {
fn finish(&self) -> u64;
fn write(&mut self, bytes: &[u8]);
fn write_u8(&mut self, i: u8) {
self.write(&[i])
}
}
struct SipHasher;
impl Hasher for SipHasher {
#[inline]
fn write(&mut self, msg: &[u8]) {
loop {}
}
#[inline]
fn finish(&self) -> u64 {
0
}
}