Skip to content

Commit

Permalink
Lexer + Parser recovery & Gathering errors in sway-error (FuelLabs#2891)
Browse files Browse the repository at this point in the history
This PR achieves a couple of related things.

1. All compiler errors are moved into the common `sway-error` crate. The
`Handler` type is now also moved there, and now uses `CompileError`
instead of `ParseError`. That's also the main goal, to generalize
`Handler` to all sorts of errors, which will help solve
FuelLabs#2734 soon.

2. Using 1, we can now also emit `LexError`s into the `Handler`, so this
enables lexer recovery.

3. Lexer recovery is added for e.g., `42y8`, which fixes
FuelLabs#2865.

4. Lexer recovery is added for `'abc'` char literals, which fixes
FuelLabs#2864. The AST supports them, but
the rest of the compiler does not, so this will be useful later on, but
was good for recovery framework testing.

5. Lexer recovery is added for mismatched open/close delimiters, e.g.,
`fn foo() { )`.

6. Lexer recovery is added for unexpected closing delimiters, e.g., `fn
foo() }`.

7. Lexer recovery is added for unclosed delimiters.

8. Parser recovery is added for `let x =`, which fixes
FuelLabs#2815.

9. Misc lexer refactoring and simplifications are made.
  • Loading branch information
Centril authored Oct 10, 2022
1 parent b6b1432 commit 2c448d9
Show file tree
Hide file tree
Showing 128 changed files with 1,986 additions and 1,718 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion forc-pkg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ forc-util = { version = "0.25.2", path = "../forc-util" }
fuel-crypto = "0.6"
fuel-tx = { version = "0.18", features = ["serde"] }
git2 = { version = "0.14", features = ["vendored-libgit2", "vendored-openssl"] }
hex = "0.4.3"
hex = "0.4.3"
petgraph = { version = "0.6", features = ["serde-1"] }
semver = { version = "1.0", features = ["serde"] }
serde = { version = "1.0", features = ["derive"] }
serde_ignored = "0.1"
serde_json = "1.0"
sway-core = { version = "0.25.2", path = "../sway-core" }
sway-error = { version = "0.25.2", path = "../sway-error" }
sway-types = { version = "0.25.2", path = "../sway-types" }
sway-utils = { version = "0.25.2", path = "../sway-utils" }
toml = "0.5"
Expand Down
3 changes: 2 additions & 1 deletion forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ use sway_core::{
language::parsed::{ParseProgram, TreeType},
semantic_analysis::namespace,
source_map::SourceMap,
BytecodeOrLib, CompileError, CompileResult, TyProgram,
BytecodeOrLib, CompileResult, TyProgram,
};
use sway_error::error::CompileError;
use sway_types::{Ident, JsonABIProgram, JsonTypeApplication, JsonTypeDeclaration};
use sway_utils::constants;
use tracing::{info, warn};
Expand Down
1 change: 1 addition & 0 deletions forc-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ansi_term = "0.12"
anyhow = "1"
dirs = "3.0.2"
sway-core = { version = "0.25.2", path = "../sway-core" }
sway-error = { version = "0.25.2", path = "../sway-error" }
sway-types = { version = "0.25.2", path = "../sway-types" }
sway-utils = { version = "0.25.2", path = "../sway-utils" }
tracing = "0.1"
Expand Down
11 changes: 6 additions & 5 deletions forc-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::str;
use std::{env, io};
use sway_core::{error::LineCol, language::parsed::TreeType, CompileError, CompileWarning};
use sway_types::Spanned;
use sway_core::{language::parsed::TreeType, CompileWarning};
use sway_error::error::CompileError;
use sway_types::{LineCol, Spanned};
use sway_utils::constants;
use tracing::{Level, Metadata};
use tracing_subscriber::{
Expand Down Expand Up @@ -227,7 +228,7 @@ fn println_std_err(txt: &str, color: Colour) {
tracing::error!("{}", color.paint(txt));
}

fn format_err(err: &sway_core::CompileError) {
fn format_err(err: &CompileError) {
let span = err.span();
let input = span.input();
let path = err.path();
Expand All @@ -243,7 +244,7 @@ fn format_err(err: &sway_core::CompileError) {
annotation_type: AnnotationType::Error,
});

let (mut start, end) = err.line_col();
let (mut start, end) = err.span().line_col();
let input = construct_window(&mut start, end, &mut start_pos, &mut end_pos, input);
let slices = vec![Slice {
source: input,
Expand Down Expand Up @@ -295,7 +296,7 @@ fn format_warning(err: &sway_core::CompileWarning) {
end_pos += 1;
}

let (mut start, end) = err.line_col();
let (mut start, end) = err.span.line_col();
let input = construct_window(&mut start, end, &mut start_pos, &mut end_pos, input);
let snippet = Snippet {
title: Some(Annotation {
Expand Down
1 change: 1 addition & 0 deletions sway-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ serde = { version = "1.0", features = ["derive"] }
sha2 = "0.9"
smallvec = "1.7"
sway-ast = { version = "0.25.2", path = "../sway-ast" }
sway-error = { version = "0.25.2", path = "../sway-error" }
sway-ir = { version = "0.25.2", path = "../sway-ir" }
sway-parse = { version = "0.25.2", path = "../sway-parse" }
sway-types = { version = "0.25.2", path = "../sway-types" }
Expand Down
4 changes: 2 additions & 2 deletions sway-core/src/asm_generation/asm_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ use crate::{
size_bytes_in_words, size_bytes_round_up_to_word_alignment,
};

use sway_error::error::CompileError;
use sway_ir::*;
use sway_types::{span::Span, Spanned};

use fuel_crypto::Hasher;

use std::{collections::HashMap, sync::Arc};

use either::Either;
use std::{collections::HashMap, sync::Arc};

pub(super) struct AsmBuilder<'ir> {
// Data section is used by the rest of code gen to layout const memory.
Expand Down
2 changes: 2 additions & 0 deletions sway-core/src/asm_generation/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::asm_lang::allocated_ops::{AllocatedOp, AllocatedOpcode};
use crate::asm_lang::*;
use crate::error::*;

use sway_error::error::CompileError;

/// Checks for disallowed opcodes in non-contract code.
/// i.e., if this is a script or predicate, we can't use certain contract opcodes.
/// See https://github.com/FuelLabs/sway/issues/350 for details.
Expand Down
1 change: 1 addition & 0 deletions sway-core/src/asm_generation/finalized_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::asm_lang::allocated_ops::AllocatedOpcode;
use crate::error::*;
use crate::source_map::SourceMap;

use sway_error::error::CompileError;
use sway_types::span::Span;

use either::Either;
Expand Down
3 changes: 2 additions & 1 deletion sway-core/src/asm_generation/from_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use super::{
DataId, DataSection,
};

use crate::{error::*, BuildConfig};
use crate::{err, ok, BuildConfig, CompileResult, CompileWarning};

use sway_error::error::CompileError;
use sway_ir::*;

pub fn compile_ir_to_asm(
Expand Down
1 change: 1 addition & 0 deletions sway-core/src/asm_lang/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::{
Ident,
};

use sway_error::error::CompileError;
use sway_types::{span::Span, Spanned};

use either::Either;
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/asm_lang/virtual_immediate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error::*;
use sway_error::error::CompileError;
use sway_types::span::Span;

use std::convert::TryInto;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
use crate::{
control_flow_analysis::*,
declaration_engine::declaration_engine::{de_get_function, de_get_impl_trait},
error::*,
language::CallPath,
semantic_analysis::*,
type_system::*,
};
use petgraph::prelude::NodeIndex;
use sway_error::error::CompileError;
use sway_types::{ident::Ident, span::Span, Spanned};

impl ControlFlowGraph {
Expand Down
5 changes: 3 additions & 2 deletions sway-core/src/control_flow_analysis/dead_code_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ use crate::{
TyIntrinsicFunctionKind, TyStorageDeclaration,
},
type_system::{to_typeinfo, TypeInfo},
CompileError, CompileWarning, Ident, Warning,
CompileWarning, Warning,
};
use petgraph::{prelude::NodeIndex, visit::Dfs};
use std::collections::BTreeSet;
use sway_types::{span::Span, Spanned};
use sway_error::error::CompileError;
use sway_types::{span::Span, Ident, Spanned};

impl ControlFlowGraph {
pub(crate) fn find_dead_code(&self) -> Vec<CompileWarning> {
Expand Down
Loading

0 comments on commit 2c448d9

Please sign in to comment.