/* Read the full contents of the file FILENAME and return them in a vector.
FIXME: platform specific. */
-std::vector<uint8_t>
+tl::optional<std::vector<uint8_t>>
load_file_bytes (location_t invoc_locus, const char *filename)
{
RAIIFile file_wrap (filename);
if (file_wrap.get_raw () == nullptr)
{
rust_error_at (invoc_locus, "cannot open filename %s: %m", filename);
- return std::vector<uint8_t> ();
+ return tl::nullopt;
}
FILE *f = file_wrap.get_raw ();
std::vector<uint8_t> buf (fsize);
- if (fread (&buf[0], fsize, 1, f) != 1)
+ if (fsize > 0 && fread (&buf[0], fsize, 1, f) != 1)
{
rust_error_at (invoc_locus, "error reading file %s: %m", filename);
return std::vector<uint8_t> ();
std::string target_filename
= source_relative_path (lit_expr->as_string (), invoc_locus);
- std::vector<uint8_t> bytes
- = load_file_bytes (invoc_locus, target_filename.c_str ());
+ auto maybe_bytes = load_file_bytes (invoc_locus, target_filename.c_str ());
+
+ if (!maybe_bytes.has_value ())
+ return AST::Fragment::create_error ();
+
+ std::vector<uint8_t> bytes = maybe_bytes.value ();
/* Is there a more efficient way to do this? */
std::vector<std::unique_ptr<AST::Expr>> elts;
std::string target_filename
= source_relative_path (lit_expr->as_string (), invoc_locus);
- std::vector<uint8_t> bytes
- = load_file_bytes (invoc_locus, target_filename.c_str ());
+ auto maybe_bytes = load_file_bytes (invoc_locus, target_filename.c_str ());
+
+ if (!maybe_bytes.has_value ())
+ return AST::Fragment::create_error ();
+
+ std::vector<uint8_t> bytes = maybe_bytes.value ();
/* FIXME: reuse lexer */
int expect_single = 0;
if (expect_single)
rust_error_at (invoc_locus, "%s was not a valid utf-8 file",
target_filename.c_str ());
- else
+ else if (!bytes.empty ())
str = std::string ((const char *) &bytes[0], bytes.size ());
+ else
+ return tl::nullopt;
auto node = AST::SingleASTNode (make_string (invoc_locus, str));
auto str_tok = make_token (Token::make_string (invoc_locus, std::move (str)));