Replace Vec with Box in Path.generic_args field

This commit is contained in:
Lukas Wirth 2021-11-20 16:37:41 +01:00
parent ceaec9d866
commit 91def936bc
3 changed files with 15 additions and 20 deletions

View file

@ -40,7 +40,7 @@ pub type LabelId = Idx<Label>;
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub enum Literal { pub enum Literal {
String(String), String(Box<str>),
ByteString(Box<[u8]>), ByteString(Box<[u8]>),
Char(char), Char(char),
Bool(bool), Bool(bool),

View file

@ -22,7 +22,7 @@ pub struct ModPath {
segments: Vec<Name>, segments: Vec<Name>,
} }
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum PathKind { pub enum PathKind {
Plain, Plain,
/// `self::` is `Super(0)` /// `self::` is `Super(0)`
@ -119,7 +119,7 @@ pub struct Path {
type_anchor: Option<Interned<TypeRef>>, type_anchor: Option<Interned<TypeRef>>,
mod_path: Interned<ModPath>, mod_path: Interned<ModPath>,
/// Invariant: the same len as `self.mod_path.segments` /// Invariant: the same len as `self.mod_path.segments`
generic_args: Vec<Option<Interned<GenericArgs>>>, generic_args: Box<[Option<Interned<GenericArgs>>]>,
} }
/// Generic arguments to a path segment (e.g. the `i32` in `Option<i32>`). This /// Generic arguments to a path segment (e.g. the `i32` in `Option<i32>`). This
@ -171,9 +171,9 @@ impl Path {
/// Converts a known mod path to `Path`. /// Converts a known mod path to `Path`.
pub fn from_known_path( pub fn from_known_path(
path: ModPath, path: ModPath,
generic_args: Vec<Option<Interned<GenericArgs>>>, generic_args: impl Into<Box<[Option<Interned<GenericArgs>>]>>,
) -> Path { ) -> Path {
Path { type_anchor: None, mod_path: Interned::new(path), generic_args } Path { type_anchor: None, mod_path: Interned::new(path), generic_args: generic_args.into() }
} }
pub fn kind(&self) -> &PathKind { pub fn kind(&self) -> &PathKind {
@ -187,7 +187,7 @@ impl Path {
pub fn segments(&self) -> PathSegments<'_> { pub fn segments(&self) -> PathSegments<'_> {
PathSegments { PathSegments {
segments: self.mod_path.segments.as_slice(), segments: self.mod_path.segments.as_slice(),
generic_args: self.generic_args.as_slice(), generic_args: &self.generic_args,
} }
} }
@ -205,14 +205,14 @@ impl Path {
self.mod_path.kind.clone(), self.mod_path.kind.clone(),
self.mod_path.segments[..self.mod_path.segments.len() - 1].iter().cloned(), self.mod_path.segments[..self.mod_path.segments.len() - 1].iter().cloned(),
)), )),
generic_args: self.generic_args[..self.generic_args.len() - 1].to_vec(), generic_args: self.generic_args[..self.generic_args.len() - 1].to_vec().into(),
}; };
Some(res) Some(res)
} }
pub fn is_self_type(&self) -> bool { pub fn is_self_type(&self) -> bool {
self.type_anchor.is_none() self.type_anchor.is_none()
&& self.generic_args == [None] && *self.generic_args == [None]
&& self.mod_path.as_ident() == Some(&name!(Self)) && self.mod_path.as_ident() == Some(&name!(Self))
} }
} }
@ -286,7 +286,7 @@ impl From<Name> for Path {
Path { Path {
type_anchor: None, type_anchor: None,
mod_path: Interned::new(ModPath::from_segments(PathKind::Plain, iter::once(name))), mod_path: Interned::new(ModPath::from_segments(PathKind::Plain, iter::once(name))),
generic_args: vec![None], generic_args: Box::new([None]),
} }
} }
} }

View file

@ -70,18 +70,13 @@ pub(super) fn lower_path(mut path: ast::Path, ctx: &LowerCtx) -> Option<Path> {
} }
// <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo // <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo
Some(trait_ref) => { Some(trait_ref) => {
let path = Path::from_src(trait_ref.path()?, ctx)?; let Path { mod_path, generic_args: path_generic_args, .. } =
let mod_path = (*path.mod_path).clone(); Path::from_src(trait_ref.path()?, ctx)?;
let num_segments = path.mod_path.segments.len(); let num_segments = mod_path.segments.len();
kind = mod_path.kind; kind = mod_path.kind;
let mut prefix_segments = mod_path.segments; segments.extend(mod_path.segments.iter().cloned().rev());
prefix_segments.reverse(); generic_args.extend(path_generic_args.iter().cloned().rev());
segments.extend(prefix_segments);
let mut prefix_args = path.generic_args;
prefix_args.reverse();
generic_args.extend(prefix_args);
// Insert the type reference (T in the above example) as Self parameter for the trait // Insert the type reference (T in the above example) as Self parameter for the trait
let last_segment = let last_segment =
@ -139,7 +134,7 @@ pub(super) fn lower_path(mut path: ast::Path, ctx: &LowerCtx) -> Option<Path> {
} }
let mod_path = Interned::new(ModPath::from_segments(kind, segments)); let mod_path = Interned::new(ModPath::from_segments(kind, segments));
return Some(Path { type_anchor, mod_path, generic_args }); return Some(Path { type_anchor, mod_path, generic_args: generic_args.into() });
fn qualifier(path: &ast::Path) -> Option<ast::Path> { fn qualifier(path: &ast::Path) -> Option<ast::Path> {
if let Some(q) = path.qualifier() { if let Some(q) = path.qualifier() {