From 043f366764ed5fd46963d92d28ba3a77417e7e7c Mon Sep 17 00:00:00 2001 From: Glyphack Date: Sat, 30 Sep 2023 17:19:32 +0200 Subject: [PATCH] Silent undefined name errors --- enderpy/src/main.rs | 2 +- parser/src/parser/parser.rs | 26 ++++++-- ...er__parser__parser__tests__comment-12.snap | 2 +- ..._parser__tests__complete@functions.py.snap | 4 +- ...ser__tests__complete@input_program.py.snap | 66 +++++++++---------- ...n_parser__parser__parser__tests__if-2.snap | 2 +- ...n_parser__parser__parser__tests__if-3.snap | 4 +- ...n_parser__parser__parser__tests__if-4.snap | 2 +- ...n_parser__parser__parser__tests__if-5.snap | 2 +- ...n_parser__parser__parser__tests__if-6.snap | 2 +- ...n_parser__parser__parser__tests__if-7.snap | 2 +- ...hon_parser__parser__parser__tests__if.snap | 2 +- typechecker/src/build.rs | 6 +- typechecker/src/semantic_analyzer.rs | 13 ++-- typechecker/src/type_check/checker.rs | 12 +--- typechecker/src/type_check/type_evaluator.rs | 15 +++-- ...hecker__build__tests__type_check_list.snap | 1 - ...r__build__tests__type_check_undefined.snap | 4 +- ...hecker__build__tests__undefined_names.snap | 7 +- 19 files changed, 84 insertions(+), 90 deletions(-) diff --git a/enderpy/src/main.rs b/enderpy/src/main.rs index e2f77df0..a4adcae7 100644 --- a/enderpy/src/main.rs +++ b/enderpy/src/main.rs @@ -31,7 +31,7 @@ fn symbols(path: &PathBuf) -> std::result::Result<(), anyhow::Error> { }; let dir_of_path = path.parent().unwrap(); let python_executable = Some( get_python_executable()? ); - let settings = Settings { debug: true, root: dir_of_path.to_path_buf(), import_discovery: ImportDiscovery { python_executable } }; + let settings = Settings { debug: false, root: dir_of_path.to_path_buf(), import_discovery: ImportDiscovery { python_executable } }; let mut manager = BuildManager::new(vec![initial_source], settings); manager.build(); diff --git a/parser/src/parser/parser.rs b/parser/src/parser/parser.rs index 2b5bcc99..1b884702 100644 --- a/parser/src/parser/parser.rs +++ b/parser/src/parser/parser.rs @@ -167,7 +167,7 @@ impl Parser { } match token { Err(err) => { - println!("Error: {:?}", err); + // println!("Error: {:#?}", err); self.bump_any(); } Ok(token) => { @@ -278,9 +278,24 @@ impl Parser { if self.cur_kind() == Kind::Indent { let node = self.start_node(); let kind = self.cur_kind(); - self.bump_any(); - println!("Error: {:?}", self.cur_token()); - return Err(self.unepxted_token(node, kind).err().unwrap()); + return Err(self.unexpected_token_new( + node, + vec![ + Kind::Assert, + Kind::Pass, + Kind::Del, + Kind::Return, + Kind::Yield, + Kind::Raise, + Kind::Break, + Kind::Continue, + Kind::Import, + Kind::From, + Kind::Global, + Kind::Nonlocal, + ], + "Unexpted indent", + )); } else { self.parse_assignment_or_expression_statement() } @@ -345,7 +360,6 @@ impl Parser { while self.eat(Kind::WhiteSpace) || self.eat(Kind::Comment) {} if !matches!(self.cur_kind(), Kind::NewLine | Kind::SemiColon | Kind::Eof) { - println!("stmt: {:?}", stmt); let node = self.finish_node(node); let kind = self.cur_kind(); let err = ParsingError::InvalidSyntax { @@ -362,8 +376,8 @@ impl Parser { } fn parse_if_statement(&mut self) -> Result { - self.bump(Kind::If); let node = self.start_node(); + self.expect(Kind::If)?; let test = Box::new(self.parse_named_expression()?); self.expect(Kind::Colon)?; let body = self.parse_suite()?; diff --git a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__comment-12.snap b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__comment-12.snap index ae20cbfd..9c93470e 100644 --- a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__comment-12.snap +++ b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__comment-12.snap @@ -11,7 +11,7 @@ Module { IfStatement( If { node: Node { - start: 3, + start: 0, end: 12, }, test: Name( diff --git a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__complete@functions.py.snap b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__complete@functions.py.snap index cbbb2748..ea78b72a 100644 --- a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__complete@functions.py.snap +++ b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__complete@functions.py.snap @@ -101,7 +101,7 @@ Module { IfStatement( If { node: Node { - start: 99, + start: 96, end: 342, }, test: Compare( @@ -189,7 +189,7 @@ Module { IfStatement( If { node: Node { - start: 180, + start: 177, end: 320, }, test: Compare( diff --git a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__complete@input_program.py.snap b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__complete@input_program.py.snap index 51de1a10..8936a8cf 100644 --- a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__complete@input_program.py.snap +++ b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__complete@input_program.py.snap @@ -101,7 +101,7 @@ Module { IfStatement( If { node: Node { - start: 99, + start: 96, end: 342, }, test: Compare( @@ -189,7 +189,7 @@ Module { IfStatement( If { node: Node { - start: 180, + start: 177, end: 320, }, test: Compare( @@ -1352,7 +1352,7 @@ Module { IfStatement( If { node: Node { - start: 1030, + start: 1027, end: 1191, }, test: Name( @@ -1733,7 +1733,7 @@ Module { IfStatement( If { node: Node { - start: 1313, + start: 1310, end: 1452, }, test: Name( @@ -2119,7 +2119,7 @@ Module { IfStatement( If { node: Node { - start: 1669, + start: 1666, end: 1800, }, test: Compare( @@ -2502,7 +2502,7 @@ Module { IfStatement( If { node: Node { - start: 1993, + start: 1990, end: 2484, }, test: BoolOp( @@ -3470,7 +3470,7 @@ Module { IfStatement( If { node: Node { - start: 3356, + start: 3353, end: 3712, }, test: Compare( @@ -3740,7 +3740,7 @@ Module { IfStatement( If { node: Node { - start: 3715, + start: 3712, end: 3800, }, test: Name( @@ -3844,7 +3844,7 @@ Module { IfStatement( If { node: Node { - start: 3803, + start: 3800, end: 4346, }, test: Name( @@ -3955,7 +3955,7 @@ Module { IfStatement( If { node: Node { - start: 3903, + start: 3900, end: 4203, }, test: Name( @@ -4096,7 +4096,7 @@ Module { IfStatement( If { node: Node { - start: 4206, + start: 4203, end: 4309, }, test: Compare( @@ -4334,7 +4334,7 @@ Module { IfStatement( If { node: Node { - start: 5596, + start: 5593, end: 5691, }, test: BoolOp( @@ -4731,7 +4731,7 @@ Module { IfStatement( If { node: Node { - start: 5822, + start: 5819, end: 5861, }, test: Compare( @@ -5259,7 +5259,7 @@ Module { IfStatement( If { node: Node { - start: 6101, + start: 6098, end: 6202, }, test: Compare( @@ -6019,7 +6019,7 @@ Module { IfStatement( If { node: Node { - start: 7152, + start: 7149, end: 7369, }, test: Compare( @@ -6772,7 +6772,7 @@ Module { IfStatement( If { node: Node { - start: 7802, + start: 7799, end: 8019, }, test: Compare( @@ -7049,7 +7049,7 @@ Module { IfStatement( If { node: Node { - start: 8079, + start: 8076, end: 8288, }, test: BoolOp( @@ -7678,7 +7678,7 @@ Module { IfStatement( If { node: Node { - start: 8578, + start: 8575, end: 8717, }, test: Compare( @@ -8120,7 +8120,7 @@ Module { IfStatement( If { node: Node { - start: 8963, + start: 8960, end: 9230, }, test: BoolOp( @@ -8262,7 +8262,7 @@ Module { IfStatement( If { node: Node { - start: 9233, + start: 9230, end: 9345, }, test: Compare( @@ -8916,7 +8916,7 @@ Module { IfStatement( If { node: Node { - start: 10026, + start: 10023, end: 10231, }, test: Name( @@ -9152,7 +9152,7 @@ Module { IfStatement( If { node: Node { - start: 10234, + start: 10231, end: 10337, }, test: Compare( @@ -9499,7 +9499,7 @@ Module { IfStatement( If { node: Node { - start: 10572, + start: 10569, end: 10703, }, test: Compare( @@ -9892,7 +9892,7 @@ Module { IfStatement( If { node: Node { - start: 10909, + start: 10906, end: 11008, }, test: Compare( @@ -10283,7 +10283,7 @@ Module { IfStatement( If { node: Node { - start: 11360, + start: 11357, end: 11409, }, test: Compare( @@ -10420,7 +10420,7 @@ Module { IfStatement( If { node: Node { - start: 11495, + start: 11492, end: 11636, }, test: Name( @@ -10618,7 +10618,7 @@ Module { IfStatement( If { node: Node { - start: 11639, + start: 11636, end: 11832, }, test: Compare( @@ -10869,7 +10869,7 @@ Module { IfStatement( If { node: Node { - start: 12135, + start: 12132, end: 12367, }, test: Compare( @@ -12432,7 +12432,7 @@ Module { IfStatement( If { node: Node { - start: 13846, + start: 13843, end: 13966, }, test: Compare( @@ -13184,7 +13184,7 @@ Module { IfStatement( If { node: Node { - start: 14516, + start: 14513, end: 14577, }, test: Compare( @@ -13318,7 +13318,7 @@ Module { IfStatement( If { node: Node { - start: 14635, + start: 14632, end: 15273, }, test: Compare( @@ -13406,7 +13406,7 @@ Module { IfStatement( If { node: Node { - start: 14747, + start: 14744, end: 15195, }, test: Compare( @@ -13699,7 +13699,7 @@ Module { IfStatement( If { node: Node { - start: 15198, + start: 15195, end: 15251, }, test: Compare( diff --git a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-2.snap b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-2.snap index ddbdddcc..a0c691da 100644 --- a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-2.snap +++ b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-2.snap @@ -11,7 +11,7 @@ Module { IfStatement( If { node: Node { - start: 3, + start: 0, end: 14, }, test: Name( diff --git a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-3.snap b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-3.snap index c90aa72d..b0e84d49 100644 --- a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-3.snap +++ b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-3.snap @@ -11,7 +11,7 @@ Module { IfStatement( If { node: Node { - start: 3, + start: 0, end: 20, }, test: Name( @@ -61,7 +61,7 @@ Module { IfStatement( If { node: Node { - start: 23, + start: 20, end: 41, }, test: Name( diff --git a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-4.snap b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-4.snap index da3ff5dc..1640d983 100644 --- a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-4.snap +++ b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-4.snap @@ -11,7 +11,7 @@ Module { IfStatement( If { node: Node { - start: 3, + start: 0, end: 19, }, test: Name( diff --git a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-5.snap b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-5.snap index cd459865..3b57f44e 100644 --- a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-5.snap +++ b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-5.snap @@ -11,7 +11,7 @@ Module { IfStatement( If { node: Node { - start: 3, + start: 0, end: 27, }, test: Compare( diff --git a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-6.snap b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-6.snap index be81ea3e..f63027b8 100644 --- a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-6.snap +++ b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-6.snap @@ -11,7 +11,7 @@ Module { IfStatement( If { node: Node { - start: 3, + start: 0, end: 65, }, test: Compare( diff --git a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-7.snap b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-7.snap index db37ea88..59f89252 100644 --- a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-7.snap +++ b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if-7.snap @@ -11,7 +11,7 @@ Module { IfStatement( If { node: Node { - start: 3, + start: 0, end: 93, }, test: Compare( diff --git a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if.snap b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if.snap index 186f280d..0a1a2e03 100644 --- a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if.snap +++ b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__if.snap @@ -11,7 +11,7 @@ Module { IfStatement( If { node: Node { - start: 3, + start: 0, end: 10, }, test: Name( diff --git a/typechecker/src/build.rs b/typechecker/src/build.rs index 29515e89..09d5aa75 100644 --- a/typechecker/src/build.rs +++ b/typechecker/src/build.rs @@ -54,11 +54,13 @@ impl BuildManager { modules.insert(mod_name, State::new(file)); } + let mut builder = Builder::new(); if options.debug { - let mut builder = Builder::new(); builder.filter(None, log::LevelFilter::Debug); - builder.init(); + } else { + builder.filter(None, log::LevelFilter::Warn); } + builder.init(); BuildManager { errors: vec![], diff --git a/typechecker/src/semantic_analyzer.rs b/typechecker/src/semantic_analyzer.rs index 64df10ec..b1105d61 100644 --- a/typechecker/src/semantic_analyzer.rs +++ b/typechecker/src/semantic_analyzer.rs @@ -1,6 +1,6 @@ use enderpy_python_parser::ast::Expression; use enderpy_python_parser as parser; -use log::info; +use log::{debug}; use parser::ast::Statement; use crate::{ @@ -43,10 +43,6 @@ impl SemanticAnalyzer { self.globals.add_symbol(symbol_node) } - fn report_unresolved_reference(&mut self) { - self.errors.push(format!("cannot resolve reference {}", "")) - } - fn current_scope(&self) -> &SymbolTableType { return self.globals.current_scope_type(); } @@ -84,10 +80,9 @@ impl SemanticAnalyzer { ) } } - Expression::Attribute(_) => print!( - "Ignoring attribute assingment. See https://github.com/Glyphack/enderpy/issues/157" - ), - _ => info!("Ignoring assignment to {:?}", target) + Expression::Attribute(_) => {}, + // TODO: Add oher expressions that can be assigned + _ => {} } } diff --git a/typechecker/src/type_check/checker.rs b/typechecker/src/type_check/checker.rs index b105e781..69812935 100644 --- a/typechecker/src/type_check/checker.rs +++ b/typechecker/src/type_check/checker.rs @@ -497,17 +497,7 @@ impl<'a> TraversalVisitor for TypeChecker<'a> { .get_symbol_node_type(symbol, n.node.start) .unwrap_or(PythonType::Unknown); let value_type = self.infer_expr_type(&_a.value, true); - if !is_reassignment_valid(&prev_target_type, &value_type) { - let msg = format!( - "Cannot assign type '{}' to variable of type '{}'", - value_type, prev_target_type - ); - self.errors.push(TypeCheckError { - msg, - start: n.node.start, - end: n.node.end, - }); - } + // TODO: Check reassignment } } _ => {}, diff --git a/typechecker/src/type_check/type_evaluator.rs b/typechecker/src/type_check/type_evaluator.rs index bfe018eb..f269dd09 100755 --- a/typechecker/src/type_check/type_evaluator.rs +++ b/typechecker/src/type_check/type_evaluator.rs @@ -38,10 +38,11 @@ impl TypeEvaluator { position: usize, ) -> Result { let decl = symbol - .declaration_until_position(position) - .ok_or_else(|| miette!("symbol {} is not defined", symbol.name))?; - self.get_type_from_declaration(decl) - .map_err(|e| miette!("cannot infer type for symbol {}: {}", symbol.name, e)) + .declaration_until_position(position); + match decl { + Some(decl) => self.get_type_from_declaration(&decl), + None => Ok(PythonType::Unknown), + } } pub fn get_type(&self, expr: &ast::Expression) -> Result { match expr { @@ -69,7 +70,7 @@ impl TypeEvaluator { self.infer_type_from_symbol_table(n.id.as_str(), n.node.start)?; match f_type { PythonType::Callable(callable_type) => Ok(callable_type.return_type), - _ => bail!("{} is not callable", n.id), + _ => Ok(PythonType::Unknown), } } ast::Expression::Attribute(_a) => Ok(PythonType::Unknown), @@ -199,7 +200,7 @@ impl TypeEvaluator { } else if let Some(source) = &v.inferred_type_source { self.get_type(source) } else { - bail!("var declaration must have a type annotation or inferred type") + Ok(PythonType::Unknown) } } Declaration::Function(f) => { @@ -227,7 +228,7 @@ impl TypeEvaluator { fn infer_type_from_symbol_table(&self, name: &str, position: usize) -> Result { match self.symbol_table.lookup_in_scope(name) { Some(symbol) => self.get_symbol_node_type(symbol, position), - None => bail!("undefined name {}", name), + None => Ok(PythonType::Unknown), } } diff --git a/typechecker/testdata/output/enderpy_python_type_checker__build__tests__type_check_list.snap b/typechecker/testdata/output/enderpy_python_type_checker__build__tests__type_check_list.snap index 194eebc1..23cb2d72 100644 --- a/typechecker/testdata/output/enderpy_python_type_checker__build__tests__type_check_list.snap +++ b/typechecker/testdata/output/enderpy_python_type_checker__build__tests__type_check_list.snap @@ -4,4 +4,3 @@ description: "a: list[int] = [1, 2, 3]\n\nb = a[0] + 1\n\nc = a[0] + a[1]\n\n# i expression: result --- 8:86:98: Operator '+' not supported for types 'Int' and 'Str' -13:152:153: Cannot assign type 'builtins.list[Unknown]' to variable of type 'builtins.list[Int]' diff --git a/typechecker/testdata/output/enderpy_python_type_checker__build__tests__type_check_undefined.snap b/typechecker/testdata/output/enderpy_python_type_checker__build__tests__type_check_undefined.snap index 4cbfcc4a..1915bd9a 100644 --- a/typechecker/testdata/output/enderpy_python_type_checker__build__tests__type_check_undefined.snap +++ b/typechecker/testdata/output/enderpy_python_type_checker__build__tests__type_check_undefined.snap @@ -3,6 +3,4 @@ source: typechecker/src/build.rs description: "a = b + 1\n\na = c()\n\n" expression: result --- -1:4:5: undefined name b -1:4:9: undefined name b -3:15:18: undefined name c + diff --git a/typechecker/testdata/output/enderpy_python_type_checker__build__tests__undefined_names.snap b/typechecker/testdata/output/enderpy_python_type_checker__build__tests__undefined_names.snap index bcf191c6..c0cf5e9f 100644 --- a/typechecker/testdata/output/enderpy_python_type_checker__build__tests__undefined_names.snap +++ b/typechecker/testdata/output/enderpy_python_type_checker__build__tests__undefined_names.snap @@ -3,9 +3,4 @@ source: typechecker/src/build.rs description: "# undefined name\nprint(undef_name)\n\n# undefined name in function\ndef func():\n print(undef_name)\n\n# undefined name in class\nclass MyClass:\n print(undef_name)\n\n# undefined name in class function\nclass MyClass:\n def func(self):\n print(undef_name)\n\ncall_undefined_name()\n\nfor i in undef_name:\n pass\n\n" expression: result --- -2:23:33: undefined name undef_name -6:87:97: undefined name undef_name -10:151:161: undefined name undef_name -15:248:258: undefined name undef_name -17:261:282: undefined name call_undefined_name -19:293:303: undefined name undef_name +