Skip to content

associated_type_defaults not working correctly #35986

Closed
@mtanski

Description

@mtanski

It seams like associated_type_defaults are not working. I have an example below from a query processing engine where I'm trying to use traits and associated types to express the application level Types of the (sql like) schema.

#![feature(associated_consts)]
#![feature(associated_type_defaults)]

use std::mem;
use std::convert::Into;

// types.rs file

// Represents Type stored on disk
pub enum Type {
    UINT32,
    // ... Lots of types
}

pub trait TypeInfo {
    // type DiskType;
    type DiskType = u8;

    const ID: Type;
    const SIZE_OF: usize;
    // ... Lots of additional consts
}

pub struct UInt32;

impl TypeInfo for UInt32 {
    type DiskType = u32;
    const ID: Type = Type::UINT32;
    // Also broken, std::mem::size_of cannot be used in const expr
    const SIZE_OF: usize =  std::mem::size_of::<UInt32::DiskType>();
}

// ... Lots of types

static uint32: UInt32 = UInt32 {};

impl Into<&'static TypeInfo> for Type {
    fn into(self) -> &'static TypeInfo {
        match self {
            UINT32 => &uint32,
        }
    }
}

// consuming file

fn main() {
    let attr = Type::UINT32;
    let info = attr.info();
}

playpen link: https://play.rust-lang.org/?gist=2d2295013a3b9bb859b3e04bc1b9dc18&version=nightly&backtrace=0

leads to the result of:

error[E0191]: the value of the associated type `DiskType` (from the trait `TypeInfo`) must be specified
  --> <anon>:34:20
   |
34 | impl Into<&'static TypeInfo> for Type {
   |                    ^^^^^^^^ missing associated type `DiskType` value

error[E0191]: the value of the associated type `DiskType` (from the trait `TypeInfo`) must be specified
  --> <anon>:35:31
   |
35 |     fn into(self) -> &'static TypeInfo {
   |                               ^^^^^^^^ missing associated type `DiskType` value

error: aborting due to 2 previous errors

Which is the same error you get when there is no DiskType default. I would like that case to work too, but it's somewhat out of scope of what I'm asking for here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)C-bugCategory: This is a bug.T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions