Remove qualified name

This commit is contained in:
Charlie Marsh
2023-07-26 14:44:46 -04:00
parent 43eb51b15a
commit 5dbe4e4653
17 changed files with 443 additions and 145 deletions

View File

@@ -1,11 +1,12 @@
use crate::{nodes, Expr};
use smallvec::{smallvec, SmallVec};
use crate::{nodes, Expr};
/// A representation of a qualified name, like `typing.List`.
pub type CallPath<'a> = SmallVec<[&'a str; 8]>;
/// Convert an `Expr` to its [`CallPath`] segments (like `["typing", "List"]`).
pub fn collect_head_path(expr: &Expr) -> Option<(&ast::ExprName, CallPath)> {
pub fn collect_head_path(expr: &Expr) -> Option<(&nodes::ExprName, CallPath)> {
// Unroll the loop up to eight times, to match the maximum number of expected attributes.
// In practice, unrolling appears to give about a 4x speed-up on this hot path.
let attr1 = match expr {

View File

@@ -899,18 +899,21 @@ pub fn from_relative_import_parts<'a>(
level: Option<u32>,
module: Option<&'a str>,
member: &'a str,
) -> CallPath<'a> {
) -> Option<CallPath<'a>> {
let mut call_path: CallPath = SmallVec::with_capacity(module_path.len() + 1);
// Start with the module path.
call_path.extend(module_path.iter().map(String::as_str));
// Remove segments based on the number of dots.
for _ in 0..level.unwrap_or(0) {
if call_path.is_empty() {
return SmallVec::new();
if let Some(level) = level {
if level > 0 {
call_path.extend(module_path.iter().map(String::as_str));
for _ in 0..level {
if call_path.is_empty() {
return None;
}
call_path.pop();
}
}
call_path.pop();
}
// Add the remaining segments.
@@ -921,7 +924,7 @@ pub fn from_relative_import_parts<'a>(
// Add the member.
call_path.push(member);
call_path
Some(call_path)
}
/// Given an imported module (based on its relative import level and module name), return the