Skip to content

Commit

Permalink
Silent undefined name errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack committed Sep 30, 2023
1 parent 3ea287a commit 043f366
Show file tree
Hide file tree
Showing 19 changed files with 84 additions and 90 deletions.
2 changes: 1 addition & 1 deletion enderpy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
26 changes: 20 additions & 6 deletions parser/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl Parser {
}
match token {
Err(err) => {
println!("Error: {:?}", err);
// println!("Error: {:#?}", err);
self.bump_any();
}
Ok(token) => {
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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 {
Expand All @@ -362,8 +376,8 @@ impl Parser {
}

fn parse_if_statement(&mut self) -> Result<Statement, ParsingError> {
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()?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Module {
IfStatement(
If {
node: Node {
start: 3,
start: 0,
end: 12,
},
test: Name(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Module {
IfStatement(
If {
node: Node {
start: 99,
start: 96,
end: 342,
},
test: Compare(
Expand Down Expand Up @@ -189,7 +189,7 @@ Module {
IfStatement(
If {
node: Node {
start: 180,
start: 177,
end: 320,
},
test: Compare(
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Module {
IfStatement(
If {
node: Node {
start: 3,
start: 0,
end: 14,
},
test: Name(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Module {
IfStatement(
If {
node: Node {
start: 3,
start: 0,
end: 20,
},
test: Name(
Expand Down Expand Up @@ -61,7 +61,7 @@ Module {
IfStatement(
If {
node: Node {
start: 23,
start: 20,
end: 41,
},
test: Name(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Module {
IfStatement(
If {
node: Node {
start: 3,
start: 0,
end: 19,
},
test: Name(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Module {
IfStatement(
If {
node: Node {
start: 3,
start: 0,
end: 27,
},
test: Compare(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Module {
IfStatement(
If {
node: Node {
start: 3,
start: 0,
end: 65,
},
test: Compare(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Module {
IfStatement(
If {
node: Node {
start: 3,
start: 0,
end: 93,
},
test: Compare(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Module {
IfStatement(
If {
node: Node {
start: 3,
start: 0,
end: 10,
},
test: Name(
Expand Down
6 changes: 4 additions & 2 deletions typechecker/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![],
Expand Down
13 changes: 4 additions & 9 deletions typechecker/src/semantic_analyzer.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
_ => {}
}
}

Expand Down
12 changes: 1 addition & 11 deletions typechecker/src/type_check/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
_ => {},
Expand Down
15 changes: 8 additions & 7 deletions typechecker/src/type_check/type_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ impl TypeEvaluator {
position: usize,
) -> Result<PythonType> {
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<PythonType> {
match expr {
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -227,7 +228,7 @@ impl TypeEvaluator {
fn infer_type_from_symbol_table(&self, name: &str, position: usize) -> Result<PythonType> {
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),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]'
Original file line number Diff line number Diff line change
Expand Up @@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 043f366

Please sign in to comment.