formatting

escape-analysis
pca006132 2021-08-05 14:55:09 +08:00
parent 3dcd846302
commit b01d0f6fbb
4 changed files with 24 additions and 40 deletions

View File

@ -22,9 +22,7 @@ impl<'a> Inferencer<'a> {
}
Ok(())
}
_ => {
self.check_expr(pattern, defined_identifiers)
}
_ => self.check_expr(pattern, defined_identifiers),
}
}
@ -46,7 +44,10 @@ impl<'a> Inferencer<'a> {
match &expr.node {
ExprKind::Name { id, .. } => {
if !defined_identifiers.contains(id) {
return Err(format!("unknown identifier {} (use before def?) at {}", id, expr.location));
return Err(format!(
"unknown identifier {} (use before def?) at {}",
id, expr.location
));
}
}
ExprKind::List { elts, .. }
@ -66,9 +67,7 @@ impl<'a> Inferencer<'a> {
ExprKind::UnaryOp { operand, .. } => {
self.check_expr(operand, defined_identifiers)?;
}
ExprKind::Compare {
left, comparators, ..
} => {
ExprKind::Compare { left, comparators, .. } => {
for elt in once(left.as_ref()).chain(comparators.iter()) {
self.check_expr(elt, defined_identifiers)?;
}
@ -83,10 +82,7 @@ impl<'a> Inferencer<'a> {
self.check_expr(orelse, defined_identifiers)?;
}
ExprKind::Slice { lower, upper, step } => {
for elt in [lower.as_ref(), upper.as_ref(), step.as_ref()]
.iter()
.flatten()
{
for elt in [lower.as_ref(), upper.as_ref(), step.as_ref()].iter().flatten() {
self.check_expr(elt, defined_identifiers)?;
}
}
@ -99,13 +95,9 @@ impl<'a> Inferencer<'a> {
}
self.check_expr(body, &defined_identifiers)?;
}
ExprKind::ListComp {
elt, generators, ..
} => {
ExprKind::ListComp { elt, generators, .. } => {
// in our type inference stage, we already make sure that there is only 1 generator
let ast::Comprehension {
target, iter, ifs, ..
} = &generators[0];
let ast::Comprehension { target, iter, ifs, .. } = &generators[0];
self.check_expr(iter, defined_identifiers)?;
let mut defined_identifiers = defined_identifiers.to_vec();
self.check_pattern(target, &mut defined_identifiers)?;
@ -113,11 +105,7 @@ impl<'a> Inferencer<'a> {
self.check_expr(term, &defined_identifiers)?;
}
}
ExprKind::Call {
func,
args,
keywords,
} => {
ExprKind::Call { func, args, keywords } => {
for expr in once(func.as_ref())
.chain(args.iter())
.chain(keywords.iter().map(|v| v.node.value.as_ref()))
@ -141,13 +129,7 @@ impl<'a> Inferencer<'a> {
defined_identifiers: &mut Vec<String>,
) -> Result<bool, String> {
match &stmt.node {
StmtKind::For {
target,
iter,
body,
orelse,
..
} => {
StmtKind::For { target, iter, body, orelse, .. } => {
self.check_expr(iter, defined_identifiers)?;
for stmt in orelse.iter() {
self.check_stmt(stmt, defined_identifiers)?;

View File

@ -7,7 +7,7 @@ pub struct FileID(u32);
#[derive(Clone, Copy, PartialEq)]
pub enum Location {
CodeRange(FileID, ast::Location),
Builtin
Builtin,
}
pub struct FileRegistry {

View File

@ -1,6 +1,6 @@
use super::location::Location;
use super::top_level::DefinitionId;
use super::typedef::Type;
use crate::top_level::DefinitionId;
use rustpython_parser::ast::Expr;
pub enum SymbolValue<'a> {

View File

@ -1,7 +1,7 @@
use std::{cell::RefCell, sync::Arc};
use std::collections::HashMap;
use std::convert::{TryInto, From};
use std::convert::{From, TryInto};
use std::iter::once;
use std::{cell::RefCell, sync::Arc};
use super::magic_methods::*;
use super::symbol_resolver::SymbolResolver;
@ -24,10 +24,7 @@ pub struct CodeLocation {
impl From<Location> for CodeLocation {
fn from(loc: Location) -> CodeLocation {
CodeLocation {
row: loc.row(),
col: loc.column()
}
CodeLocation { row: loc.row(), col: loc.column() }
}
}
@ -190,8 +187,12 @@ impl<'a> Inferencer<'a> {
params: Vec<Type>,
ret: Type,
) -> InferenceResult {
let call =
Arc::new(Call { posargs: params, kwargs: HashMap::new(), ret, fun: RefCell::new(None) });
let call = Arc::new(Call {
posargs: params,
kwargs: HashMap::new(),
ret,
fun: RefCell::new(None),
});
let call = self.unifier.add_ty(TypeEnum::TCall(vec![call].into()));
let fields = once((method, call)).collect();
let record = self.unifier.add_record(fields);
@ -333,7 +334,8 @@ impl<'a> Inferencer<'a> {
}
let arg0 = self.fold_expr(args.remove(0))?;
let ty = if let Some(arg) = args.pop() {
self.function_data.resolver
self.function_data
.resolver
.parse_type_name(&arg)
.ok_or_else(|| "error parsing type".to_string())?
} else {