Skip to content

Commit

Permalink
feat(backend/f*): report parsing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
W95Psp committed Dec 18, 2024
1 parent 42846d5 commit bd06161
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
20 changes: 19 additions & 1 deletion engine/backends/fstar/fstar_backend.ml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ module Make
struct
open Ctx

module StringToFStar = struct
let catch_parsing_error (type a b) kind span (f : a -> b) x =
try f x
with e ->
let kind =
Types.FStarParseError
{
fstar_snippet = "";
details =
"While parsing a " ^ kind ^ ", error: "
^ Base.Error.to_string_hum (Base.Error.of_exn e);
}
in
Error.raise { span; kind }

let term span = catch_parsing_error "term" span F.term_of_string
end

let doc_to_string : PPrint.document -> string =
FStar_Pprint.pretty_string 1.0 (Z.of_int ctx.line_width)

Expand Down Expand Up @@ -667,7 +685,7 @@ struct
kind = UnsupportedMacro { id = [%show: global_ident] macro };
span = e.span;
}
| Quote quote -> pquote e.span quote |> F.term_of_string
| Quote quote -> pquote e.span quote |> StringToFStar.term e.span
| _ -> .

(** Prints a `quote` to a string *)
Expand Down
10 changes: 9 additions & 1 deletion hax-types/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ impl std::fmt::Display for Diagnostics {

Kind::NonTrivialAndMutFnInput => write!(f, "The support in hax of function with one or more inputs of type `&mut _` is limited. Onlu trivial patterns are allowed there: `fn f(x: &mut (T, U)) ...` is allowed while `f((x, y): &mut (T, U))` is rejected."),

Kind::FStarParseError { fstar_snippet, details: _ } => write!(f, "The following code snippet could not be parsed as valid F*:\n```\n{fstar_snippet}\n```"),

_ => write!(f, "{:?}", self.kind),
}
}
Expand Down Expand Up @@ -135,7 +137,13 @@ pub enum Kind {
/// An hax attribute (from `hax-lib-macros`) was rejected
AttributeRejected {
reason: String,
},
} = 12,

/// A snippet of F* code could not be parsed
FStarParseError {
fstar_snippet: String,
details: String,
} = 13,
}

impl Kind {
Expand Down

0 comments on commit bd06161

Please sign in to comment.