MCPcopy Index your code
hub / github.com/RustPython/RustPython / scan_expression

Method scan_expression

crates/codegen/src/symboltable.rs:1754–2138  ·  view source on GitHub ↗
(
        &mut self,
        expression: &ast::Expr,
        context: ExpressionContext,
    )

Source from the content-addressed store, hash-verified

1752 }
1753
1754 fn scan_expression(
1755 &mut self,
1756 expression: &ast::Expr,
1757 context: ExpressionContext,
1758 ) -> SymbolTableResult {
1759 use ast::*;
1760
1761 // Check for expressions not allowed in certain contexts
1762 // (type parameters, annotations, type aliases, TypeVar bounds/defaults)
1763 if let Some(keyword) = match expression {
1764 Expr::Yield(_) | Expr::YieldFrom(_) => Some("yield"),
1765 Expr::Await(_) => Some("await"),
1766 Expr::Named(_) => Some("named"),
1767 _ => None,
1768 } {
1769 // Determine the context name for the error message
1770 // scope_info takes precedence (e.g., "a TypeVar bound")
1771 let context_name = if let Some(scope_info) = self.scope_info {
1772 Some(scope_info)
1773 } else if let Some(table) = self.tables.last()
1774 && table.typ == CompilerScope::TypeParams
1775 {
1776 Some("a type parameter")
1777 } else if self.in_annotation {
1778 Some("an annotation")
1779 } else if self.in_type_alias {
1780 Some("a type alias")
1781 } else {
1782 None
1783 };
1784
1785 if let Some(context_name) = context_name {
1786 return Err(SymbolTableError {
1787 error: format!("{keyword} expression cannot be used within {context_name}"),
1788 location: Some(
1789 self.source_file
1790 .to_source_code()
1791 .source_location(expression.range().start(), PositionEncoding::Utf8),
1792 ),
1793 });
1794 }
1795 }
1796
1797 match expression {
1798 Expr::BinOp(ExprBinOp {
1799 left,
1800 right,
1801 range: _,
1802 ..
1803 }) => {
1804 self.scan_expression(left, context)?;
1805 self.scan_expression(right, context)?;
1806 }
1807 Expr::BoolOp(ExprBoolOp {
1808 values, range: _, ..
1809 }) => {
1810 self.scan_expressions(values, context)?;
1811 }

Callers 9

scan_exprMethod · 0.80
scan_annotation_innerMethod · 0.80
scan_statementMethod · 0.80
scan_decoratorsMethod · 0.80
scan_expressionsMethod · 0.80
scan_comprehensionMethod · 0.80
scan_patternMethod · 0.80

Calls 15

source_locationMethod · 0.80
scan_expressionsMethod · 0.80
check_nameMethod · 0.80
scan_comprehensionMethod · 0.80
register_nameMethod · 0.80
line_index_startMethod · 0.80
leave_scopeMethod · 0.80
interpolationsMethod · 0.80
to_stringMethod · 0.80
SomeClass · 0.50
ErrClass · 0.50

Tested by

no test coverage detected