use super::*;
impl Checker<'_> {
pub(super) fn bind(&mut self, id: DefId, facts: Facts, state: &mut State) {
state.consumed.retain(|place, _| place.root != id);
state.bindings.insert(id, facts.clone());
self.next_bindings
.entry(id)
.and_modify(|previous| *previous = previous.meet(&facts))
.or_insert(facts);
}
pub(super) fn function_value(&self, body: Span) -> Facts {
let kind = self
.summaries
.get(&body)
.map_or(CallableKind::Once, |summary| summary.kind);
Facts {
kind: Some(kind),
targets: vec![body],
linear: kind == CallableKind::Once,
..Facts::default()
}
}
fn function(&mut self, function: &Function<'_>, outer: &State) {
let saved = (
self.current,
self.effect,
std::mem::take(&mut self.returns),
std::mem::take(&mut self.loops),
std::mem::replace(&mut self.module, function.module.clone()),
);
self.current = Some(function.body.span);
self.effect = CallableKind::Shared;
let mut state = outer.clone();
state.unreachable = false;
state.consumed.clear();
for (index, parameter) in function.parameters.iter().enumerate() {
if let Some(id) = self.definitions.resolution(parameter.span) {
let kind =
self.parameter_kind(parameter.type_annotation.as_ref(), &function.generics);
let linear = kind.is_some()
&& !self.parameter_copy(parameter.type_annotation.as_ref(), &function.generics);
self.bind(
id,
Facts {
kind,
linear,
parameter: parameter
.type_annotation
.as_ref()
.filter(|ty| {
matches!(
ty,
Type::Function { .. }
| Type::Variable(_)
| Type::BoundVariable { .. }
)
})
.map(|_| (function.body.span, index)),
..Facts::default()
},
&mut state,
);
}
}
let tail = self.statements(&function.body.statements, &mut state);
if !state.unreachable {
self.returns.push(tail);
}
let output = self
.returns
.iter()
.cloned()
.reduce(|a, b| a.meet(&b))
.unwrap_or_default();
self.next_summaries.insert(
function.body.span,
Summary {
kind: self.effect,
output,
},
);
(
self.current,
self.effect,
self.returns,
self.loops,
self.module,
) = saved;
}
fn parameter_kind(
&self,
ty: Option<&Type>,
generics: &[GenericParameter],
) -> Option<CallableKind> {
let ty = ty?;
let bounds = match ty {
Type::Variable(name) => generics
.iter()
.find(|parameter| parameter.name == *name)?
.bounds
.as_slice(),
Type::BoundVariable { bounds, .. } => bounds,
Type::Function { .. } => return None,
_ => return None,
};
bounds
.iter()
.filter_map(|bound| self.bound_kind(bound))
.max()
}
fn parameter_copy(&self, ty: Option<&Type>, generics: &[GenericParameter]) -> bool {
let Some(Type::Variable(name) | Type::BoundVariable { name, .. }) = ty else {
return false;
};
generics
.iter()
.find(|parameter| parameter.name == *name)
.is_some_and(|parameter| {
parameter.bounds.iter().any(|bound| {
self.declarations.resolve(bound, &self.module)
== Type::named("core::clone::Copy")
})
})
}
pub(super) fn statements(&mut self, statements: &[Stmt], state: &mut State) -> Facts {
let mut result = Facts::default();
for statement in statements {
if state.unreachable {
break;
}
result = Facts::default();
match statement {
Stmt::Function {
body,
name_span,
attributes,
..
} => {
if crate::ast::has_compiler_internal_attribute(attributes) {
continue;
}
if let Some(id) = self.definitions.resolution(*name_span) {
self.bind(id, self.function_value(body.span), state);
}
if let Some(function) = self.registry.functions.get(&body.span).cloned() {
self.function(&function, state);
}
}
Stmt::Impl { methods, .. } => {
for method in methods {
if let Some(function) =
self.registry.functions.get(&method.body.span).cloned()
{
self.function(&function, state);
}
}
}
Stmt::Let {
name_span,
initializer,
..
} => {
let facts = self.expression(initializer, Access::Owned, state);
if let Some(id) = self.definitions.resolution(*name_span) {
self.bind(id, facts, state);
}
}
Stmt::Expr {
expression,
terminated,
} => {
let value = self.expression(expression, Access::Owned, state);
if !terminated {
result = value;
}
}
Stmt::Return { value, .. } => {
let facts = value
.as_ref()
.map(|value| self.expression(value, Access::Owned, state))
.unwrap_or_default();
self.returns.push(facts);
state.unreachable = true;
}
Stmt::Module {
name,
statements: Some(children),
..
} => {
self.module.push(name.clone());
self.statements(children, state);
self.module.pop();
}
Stmt::While {
condition, body, ..
} => self.loop_body(Some(condition), None, body, state),
Stmt::For { iterable, body, .. } => {
self.loop_body(None, Some(iterable), body, state)
}
Stmt::Loop { body, .. } => self.loop_body(None, None, body, state),
Stmt::Break { value, .. } => {
if let Some(value) = value {
self.expression(value, Access::Owned, state);
}
if let Some((breaks, _)) = self.loops.last_mut() {
breaks.push(state.clone());
}
state.unreachable = true;
}
Stmt::Continue { .. } => {
if let Some((_, continues)) = self.loops.last_mut() {
continues.push(state.clone());
}
state.unreachable = true;
}
_ => {}
}
}
result
}
fn loop_body(
&mut self,
condition: Option<&Expr>,
iterable: Option<&Expr>,
body: &Block,
state: &mut State,
) {
if let Some(iterable) = iterable {
self.expression(iterable, Access::Owned, state);
}
let before = state.clone();
if let Some(condition) = condition {
self.expression(condition, Access::Owned, state);
}
let after_condition = state.clone();
self.loops.push((Vec::new(), Vec::new()));
self.statements(&body.statements, state);
let (mut breaks, mut backedges) = self.loops.pop().unwrap();
if !state.unreachable {
backedges.push(state.clone());
}
for edge in &backedges {
for (id, span) in &edge.consumed {
if !before.consumed.contains_key(id) && before.bindings.contains_key(&id.root) {
self.diagnostic(
"FnOnce callable may be consumed more than once by a loop",
*span,
);
}
}
}
if condition.is_some() || iterable.is_some() {
breaks.push(after_condition);
breaks.extend(backedges);
}
*state = State::merge(&breaks);
}
}