Skip to content

Commit 664a8c4

Browse files
committed
fix(engine): rewrite Self
1 parent 5ee40eb commit 664a8c4

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

engine/backends/fstar/fstar_backend.ml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,6 +2003,42 @@ let unsize_as_identity =
20032003
in
20042004
visitor#visit_item ()
20052005

2006+
(** Rewrites, in trait, local bounds refereing to `Self` into the impl expr of
2007+
kind `Self`. *)
2008+
let local_self_bound_as_self (i : AST.item) : AST.item =
2009+
match i.v with
2010+
| Trait { name; generics; _ } ->
2011+
let generic_eq (param : generic_param) (value : generic_value) =
2012+
(let* id =
2013+
match (param.kind, value) with
2014+
| GPConst _, GConst { e = LocalVar id } -> Some id
2015+
| GPType, GType (TParam id) -> Some id
2016+
| GPLifetime _, GLifetime _ -> Some param.ident
2017+
| _ -> None
2018+
in
2019+
Some ([%eq: local_ident] id param.ident))
2020+
|> Option.value ~default:false
2021+
in
2022+
let generics_eq params values =
2023+
match List.for_all2 ~f:generic_eq params values with
2024+
| List.Or_unequal_lengths.Ok v -> v
2025+
| List.Or_unequal_lengths.Unequal_lengths -> false
2026+
in
2027+
(object
2028+
inherit [_] U.Visitors.map as super
2029+
2030+
method! visit_impl_expr () ie =
2031+
match ie with
2032+
| { kind = LocalBound _; goal = { args; trait } }
2033+
when [%eq: concrete_ident] trait name
2034+
&& generics_eq generics.params args ->
2035+
{ ie with kind = Self }
2036+
| _ -> ie
2037+
end)
2038+
#visit_item
2039+
() i
2040+
| _ -> i
2041+
20062042
let apply_phases (bo : BackendOptions.t) (items : Ast.Rust.item list) :
20072043
AST.item list =
20082044
let items =
@@ -2022,6 +2058,7 @@ let apply_phases (bo : BackendOptions.t) (items : Ast.Rust.item list) :
20222058
TransformToInputLanguage.ditems items
20232059
|> List.map ~f:unsize_as_identity
20242060
|> List.map ~f:unsize_as_identity
2061+
|> List.map ~f:local_self_bound_as_self
20252062
|> List.map ~f:U.Mappers.add_typ_ascription
20262063
in
20272064
items

0 commit comments

Comments
 (0)