Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Const ty #945

Merged
merged 17 commits into from
Jan 28, 2024
Merged

Const ty #945

merged 17 commits into from
Jan 28, 2024

Conversation

Y-Nak
Copy link
Member

@Y-Nak Y-Nak commented Oct 23, 2023

This PR introduces basic const types.
Currently, there are some restrictions on the usage of const types, which will be mitigated when a type checker is implemented.

Usage

ADTs, functions, and traits can be declared with const type parameters.
The basic example would be

pub struct S<const N: u32> {
    N: u32
}

pub fn f<const N: u32>() {}

pub trait Trait<const B: bool> {}

Const type parameters can treated in the same way as normal type parameters in trait or method implementation.
e.g.,

pub trait Trait<const N: u32> {}
pub struct S<const N: u32> {}

impl<const N: u32> S<N> {
     // `foo` is defined for S with all `N`(0..u32::MAX)     
    pub fn foo(self) {}
}

// `Trait<1>` is implemented for `S<1>`, `Trait<2>` is implemented for `S<2>` respectively.
impl<const N: u32> Trait<N> for S<N> {}

// It's also possible to implement specialized traits for specialized types.
impl Trait<2> for S<1> {}

impl S<1> {
    // `bar` is defined only for `S<1>`
    fn bar(self) {}
}

Limitations

There are two major restrictions on the type system.

  1. Only bool and integral types(e.g., u8, u16, ...) are allowed for a const type.
  2. Only literals are allowed as a const type argument.

These limitations will be mitigated when we redesign type system.

MIsc

Replace fxhash with rustc_hash in parser2.

@Y-Nak Y-Nak mentioned this pull request Jan 4, 2024
3 tasks
@Y-Nak Y-Nak force-pushed the dependent-ty branch 4 times, most recently from 27b28fe to 6e4d510 Compare January 10, 2024 22:07
@Y-Nak Y-Nak force-pushed the dependent-ty branch 2 times, most recently from 2be3503 to ce9d1ab Compare January 11, 2024 13:26
@Y-Nak Y-Nak force-pushed the dependent-ty branch 2 times, most recently from c3c13bc to 01f5393 Compare January 11, 2024 21:52
@Y-Nak Y-Nak marked this pull request as ready for review January 12, 2024 17:59
@sbillig
Copy link
Collaborator

sbillig commented Jan 22, 2024

@Y-Nak Will we allow dependent functions, with a return type that depends on the value of an argument?
Eg

pub struct X<const N: u8> {}

pub fn bar(n: u8) -> X {
  return X<n>{}
}

In the current implementation, should the syntax of -> X be allowed, or should there be an error? (There currently isn't an error)

@Y-Nak Y-Nak changed the title Dependent ty Const ty Jan 22, 2024
@Y-Nak
Copy link
Member Author

Y-Nak commented Jan 22, 2024

This PR is pretty restrictive so that we can introduce the basic const generics. I should've titled this PR as basic const generics rather than dependent types(I couldn't make up my mind whether I should continue implementing a genuine dependent type system in this PR when I implemented this PR, then forgot to rename this in my mind).
So, for now, your example should be an error(I fixed this bug).

I'm trying to design a more sophisticated type system now in the separate PR, which will allow the snippet below.

pub struct X<const N: u8> {}

pub fn bar(n: u8) -> X<n> {
  return X<n>{}
}

crates/hir-analysis/src/ty/diagnostics.rs Outdated Show resolved Hide resolved
┌─ const_generics_cycle.fe:1:28
1 │ pub struct Foo<T, const U: T> {}
│ ^ recursive const parameter type is detected here
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gave up trying to trace the cycle that this situation creates, but it seems to me that it should be possible to allow this? (Not suggesting that this is important)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discussed this in the meeting; feel free to ignore. (I don't personally care whether this stays a "recursive" error, or is changed to be a 'not a primitive type' error)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed this by making the generic parameter evaluation lazy, which is the same as the ADT field evaluation approach.

Copy link
Member Author

@Y-Nak Y-Nak Jan 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The recursive type error occurs only if a parameter type depends on itself.
e.g.,

struct Foo<T, const U: U> {}

Copy link
Collaborator

@sbillig sbillig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@Y-Nak Y-Nak merged commit 1b7c7e9 into ethereum:fe-v2 Jan 28, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants