Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): bring statement causes a panic in wasi builds. #415

Merged
merged 1 commit into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions apps/wing-language-server/src/prep.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{cell::RefCell, collections::HashSet};
use std::cell::RefCell;

use tree_sitter::Tree;
use wingc::{diagnostic::Diagnostics, parser::Parser, type_check};
Expand All @@ -21,9 +21,7 @@ pub fn parse_text(source_file: &str, text: &[u8]) -> ParseResult {
}
};

let mut imports = HashSet::new();
let wing_parser = Parser {
imports: RefCell::new(&mut imports),
source: &text[..],
source_name: source_file.to_string(),
diagnostics: RefCell::new(Diagnostics::new()),
Expand Down
1 change: 0 additions & 1 deletion examples/invalid/circular-bring.w

This file was deleted.

1 change: 0 additions & 1 deletion examples/invalid/circular/mod.w

This file was deleted.

2 changes: 0 additions & 2 deletions examples/simple/bring.w

This file was deleted.

2 changes: 0 additions & 2 deletions examples/simple/modules/mod1.w

This file was deleted.

4 changes: 0 additions & 4 deletions examples/simple/modules/mod2.w

This file was deleted.

1 change: 0 additions & 1 deletion examples/simple/modules/sub/mod.w

This file was deleted.

15 changes: 0 additions & 15 deletions libs/tree-sitter-wing/test/corpus/statements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -254,18 +254,3 @@ struct Test {
)
)
)

==================================
Bring statement
==================================

bring cloud;
bring "./mod.w";

---
(source
(short_import_statement
module_name: (identifier))
(short_import_statement
module_name: (string))
)
19 changes: 0 additions & 19 deletions libs/wingc/.vscode/launch.json

This file was deleted.

3 changes: 0 additions & 3 deletions libs/wingc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,3 @@ path = "src/bin.rs"
[package.metadata]
# Currently wasm-opt is not changing the size of the wasm file, so it's just adding unnecessary time to the build.
wasm-opt = false

[profile.bench]
debug = true
5 changes: 0 additions & 5 deletions libs/wingc/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,10 @@ pub struct Constructor {

#[derive(Debug)]
pub enum Statement {
// TODO: merge these statements into a single one
Use {
module_name: Symbol, // Reference?
identifier: Option<Symbol>,
},
Bring {
module_path: String,
statements: Scope,
},
VariableDef {
var_name: Symbol,
initial_value: Expr,
Expand Down
6 changes: 0 additions & 6 deletions libs/wingc/src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,6 @@ fn scan_captures_in_scope(scope: &Scope) -> Vec<Capture> {
} => {
todo!()
}
Statement::Bring {
module_path: _,
statements,
} => {
res.extend(scan_captures_in_scope(statements));
}
Statement::Struct {
name: _,
extends: _,
Expand Down
9 changes: 0 additions & 9 deletions libs/wingc/src/jsify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,15 +405,6 @@ fn jsify_statement(statement: &Statement, out_dir: &PathBuf) -> String {
.join("\n")
)
}
Statement::Bring {
module_path,
statements,
} => format!(
"/* start bring module: {module} */\n{}\n/* end bring module: {module} */",
jsify_scope(statements, &out_dir),
module = module_path
)
.to_string(),
}
}

Expand Down
39 changes: 34 additions & 5 deletions libs/wingc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#[macro_use]
extern crate lazy_static;

use crate::parser::bring;
use ast::Scope;
use diagnostic::{print_diagnostics, DiagnosticLevel, Diagnostics};

use std::collections::HashSet;
use crate::parser::Parser;
use std::cell::RefCell;
use std::fs;
use std::path::PathBuf;

Expand All @@ -22,6 +22,36 @@ pub mod jsify;
pub mod parser;
pub mod type_check;

pub fn parse(source_file: &str) -> (Scope, Diagnostics) {
let language = tree_sitter_wing::language();
let mut parser = tree_sitter::Parser::new();
parser.set_language(language).unwrap();

let source = match fs::read(&source_file) {
Ok(source) => source,
Err(err) => {
panic!("Error reading source file: {}: {:?}", &source_file, err);
}
};

let tree = match parser.parse(&source[..], None) {
Some(tree) => tree,
None => {
panic!("Failed parsing source file: {}", source_file);
}
};

let wing_parser = Parser {
source: &source[..],
source_name: source_file.to_string(),
diagnostics: RefCell::new(Diagnostics::new()),
};

let scope = wing_parser.wingit(&tree.root_node());

(scope, wing_parser.diagnostics.into_inner())
}

pub fn type_check(scope: &mut Scope, types: &mut Types) -> Diagnostics {
scope.set_env(TypeEnv::new(None, None, false, Flight::Pre));
let mut tc = TypeChecker::new(types);
Expand All @@ -31,12 +61,11 @@ pub fn type_check(scope: &mut Scope, types: &mut Types) -> Diagnostics {
}

pub fn compile(source_file: &str, out_dir: Option<&str>) -> String {
// create a new hashmap to manage imports
let mut imports = HashSet::new();
// Create universal types collection (need to keep this alive during entire compilation)
let mut types = Types::new();
// Build our AST
let (mut scope, parse_diagnostics) = bring::bring(source_file, None, &mut imports).unwrap();
let (mut scope, parse_diagnostics) = parse(source_file);

// Type check everything and build typed symbol environment
let type_check_diagnostics = type_check(&mut scope, &mut types);

Expand Down
46 changes: 8 additions & 38 deletions libs/wingc/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
pub mod bring;

use bring::bring;
use std::cell::RefCell;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::collections::HashMap;
use std::{str, vec};
use tree_sitter::Node;

Expand All @@ -18,7 +14,6 @@ pub struct Parser<'a> {
pub source: &'a [u8],
pub source_name: String,
pub diagnostics: RefCell<Diagnostics>,
pub imports: RefCell<&'a mut HashSet<String>>,
}

impl Parser<'_> {
Expand Down Expand Up @@ -114,39 +109,14 @@ impl Parser<'_> {

fn build_statement(&self, statement_node: &Node) -> DiagnosticResult<Statement> {
match statement_node.kind() {
"short_import_statement" => {
let module_name_node = statement_node.child_by_field_name("module_name").unwrap();
if module_name_node.kind() == "identifier" {
Ok(Statement::Use {
module_name: self.node_symbol(&module_name_node)?,
identifier: if let Some(identifier) = statement_node.child_by_field_name("alias") {
Some(self.node_symbol(&identifier)?)
} else {
None
},
})
} else if module_name_node.kind() == "string" {
let bring_target = self.node_text(&module_name_node);
let bring_target = &bring_target[1..bring_target.len() - 1];
let context_dir = PathBuf::from(&self.source_name);
let context_dir = context_dir.parent().unwrap().to_str().unwrap();
let brought = bring(bring_target, Some(context_dir), *self.imports.borrow_mut());
Ok(Statement::Bring {
module_path: bring_target.to_string(),
statements: brought
.unwrap_or((
Scope {
statements: vec![],
env: None,
},
Diagnostics::new(),
))
.0,
})
"short_import_statement" => Ok(Statement::Use {
module_name: self.node_symbol(&statement_node.child_by_field_name("module_name").unwrap())?,
identifier: if let Some(identifier) = statement_node.child_by_field_name("alias") {
Some(self.node_symbol(&identifier)?)
} else {
panic!("Unexpected bring type {}", module_name_node.kind())
}
}
None
},
}),
"variable_definition_statement" => {
let type_ = if let Some(type_node) = statement_node.child_by_field_name("type") {
Some(self.build_type(&type_node)?)
Expand Down
61 changes: 0 additions & 61 deletions libs/wingc/src/parser/bring.rs

This file was deleted.

6 changes: 0 additions & 6 deletions libs/wingc/src/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,12 +900,6 @@ impl<'a> TypeChecker<'a> {
let var_type = self.resolve_reference(variable, env);
self.validate_type(exp_type, var_type, value);
}
Statement::Bring {
module_path: _,
statements: _,
} => {
// no-op
}
Statement::Use {
module_name,
identifier,
Expand Down