Add invalid UTF-8 error to use and source

Also changed the error message to be more universal.
This commit is contained in:
Jakub Žádník 2021-10-31 17:53:53 +02:00
parent f182524298
commit 73ae3daf85
2 changed files with 54 additions and 44 deletions

View File

@ -93,9 +93,9 @@ pub enum ParseError {
)] )]
UnknownCommand(#[label = "unknown command"] Span), UnknownCommand(#[label = "unknown command"] Span),
#[error("Non-UTF8 code.")] #[error("Non-UTF8 string.")]
#[diagnostic(code(nu::parser::non_utf8), url(docsrs))] #[diagnostic(code(nu::parser::non_utf8), url(docsrs))]
NonUtf8(#[label = "non-UTF8 code"] Span), NonUtf8(#[label = "non-UTF8 string"] Span),
#[error("The `{0}` command doesn't have flag `{1}`.")] #[error("The `{0}` command doesn't have flag `{1}`.")]
#[diagnostic(code(nu::parser::unknown_flag), url(docsrs))] #[diagnostic(code(nu::parser::unknown_flag), url(docsrs))]

View File

@ -502,18 +502,16 @@ pub fn parse_use(
let (import_pattern, err) = parse_import_pattern(working_set, &spans[1..]); let (import_pattern, err) = parse_import_pattern(working_set, &spans[1..]);
error = error.or(err); error = error.or(err);
let (import_pattern, exports) = if let Some(block_id) = let (import_pattern, exports) =
working_set.find_module(&import_pattern.head) if let Some(block_id) = working_set.find_module(&import_pattern.head) {
{
( (
import_pattern, import_pattern,
working_set.get_block(block_id).exports.clone(), working_set.get_block(block_id).exports.clone(),
) )
} else { } else {
// TODO: Handle invalid UTF-8 conversion
// TODO: Do not close over when loading module from file // TODO: Do not close over when loading module from file
// It could be a file // It could be a file
let module_filename = String::from_utf8_lossy(&import_pattern.head).to_string(); if let Ok(module_filename) = String::from_utf8(import_pattern.head) {
let module_path = Path::new(&module_filename); let module_path = Path::new(&module_filename);
let module_name = if let Some(stem) = module_path.file_stem() { let module_name = if let Some(stem) = module_path.file_stem() {
stem.to_string_lossy().to_string() stem.to_string_lossy().to_string()
@ -529,7 +527,8 @@ pub fn parse_use(
working_set.add_file(module_filename, &contents); working_set.add_file(module_filename, &contents);
let span_end = working_set.next_span_start(); let span_end = working_set.next_span_start();
let (block, err) = parse_module_block(working_set, Span::new(span_start, span_end)); let (block, err) =
parse_module_block(working_set, Span::new(span_start, span_end));
error = error.or(err); error = error.or(err);
let block_id = working_set.add_module(&module_name, block); let block_id = working_set.add_module(&module_name, block);
@ -547,6 +546,12 @@ pub fn parse_use(
Some(ParseError::ModuleNotFound(spans[1])), Some(ParseError::ModuleNotFound(spans[1])),
); );
} }
} else {
return (
garbage_statement(spans),
Some(ParseError::NonUtf8(spans[1])),
);
}
}; };
let exports = if import_pattern.members.is_empty() { let exports = if import_pattern.members.is_empty() {
@ -892,6 +897,11 @@ pub fn parse_source(
); );
} }
} }
} else {
return (
garbage_statement(spans),
Some(ParseError::NonUtf8(spans[1])),
);
} }
} }
return ( return (