Skip to content

Commit

Permalink
Merge pull request #597 from hacspec/allow-default-const-values-in-ge…
Browse files Browse the repository at this point in the history
…nerics

fix(engine): allow default generics
  • Loading branch information
W95Psp authored Apr 11, 2024
2 parents ae1d9ad + 92c7506 commit e8140e4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 48 deletions.
4 changes: 1 addition & 3 deletions engine/backends/fstar/fstar_backend.ml
Original file line number Diff line number Diff line change
Expand Up @@ -578,10 +578,8 @@ struct
let ident = plocal_ident p.ident in
match p.kind with
| GPLifetime _ -> Error.assertion_failure span "pgeneric_param:LIFETIME"
| GPType { default = None } ->
| GPType { default = _ } ->
{ kind; typ = F.term @@ F.AST.Name (F.lid [ "Type" ]); ident }
| GPType _ ->
Error.unimplemented span ~details:"pgeneric_param:Type with default"
| GPConst { typ } -> { kind = Explicit; typ = pty span typ; ident }

let of_generic_constraint span (nth : int) (c : generic_constraint) =
Expand Down
5 changes: 2 additions & 3 deletions engine/lib/import_thir.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1003,9 +1003,8 @@ end) : EXPR = struct
| Type { default; _ } ->
let default = Option.map ~f:(c_ty param.span) default in
GPType { default }
| Const { default = Some _; _ } ->
unimplemented [ param.span ] "c_generic_param:Const with a default"
| Const { default = None; ty } -> GPConst { typ = c_ty param.span ty }
(* Rustc always fills in const generics on use. Thus we can drop this information. *)
| Const { default = _; ty } -> GPConst { typ = c_ty param.span ty }
in
let span = Span.of_thir param.span in
let attrs = c_attrs param.attributes in
Expand Down
10 changes: 10 additions & 0 deletions test-harness/src/snapshots/toolchain__generics into-fstar.snap
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ Compiling generics v0.1.0 (WORKSPACE_ROOT/generics)
diagnostics = []

[stdout.files]
"Generics.Defaults_generics.fst" = '''
module Generics.Defaults_generics
#set-options "--fuel 0 --ifuel 1 --z3rlimit 15"
open Core
open FStar.Mul

type t_Defaults (v_T: Type) (v_N: usize) = | Defaults : t_Array v_T v_N -> t_Defaults v_T v_N

let f (_: t_Defaults Prims.unit (sz 2)) : Prims.unit = ()
'''
"Generics.fst" = '''
module Generics
#set-options "--fuel 0 --ifuel 1 --z3rlimit 15"
Expand Down
45 changes: 3 additions & 42 deletions tests/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/generics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ struct Bar;
impl Bar {
fn inherent_impl_generics<T, const N: usize>(x: [T; N]) {}
}

/// Test defaults types and constants
mod defaults_generics {
struct Defaults<T = (), const N: usize = 2>([T; N]);
fn f(_: Defaults) {}
}

0 comments on commit e8140e4

Please sign in to comment.