Skip to content

Commit

Permalink
docs and remove cast and clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Feb 8, 2024
1 parent 07a744e commit ec6b76e
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 44 deletions.
2 changes: 1 addition & 1 deletion core/src/hir/defs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Type definitions for structs, output structs, opaque structs, and enums.
use super::lifetimes::{self, LifetimeEnv};
use super::lifetimes::LifetimeEnv;
use super::{Attrs, Everywhere, IdentBuf, Method, OutputOnly, TyPosition, Type};
use crate::ast::Docs;

Expand Down
2 changes: 1 addition & 1 deletion core/src/hir/elision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
//!
//! [Nomicon]: https://doc.rust-lang.org/nomicon/lifetime-elision.html
use super::lifetimes::{self, BoundedLifetime, Lifetime, LifetimeEnv, Lifetimes, MaybeStatic};
use super::lifetimes::{BoundedLifetime, Lifetime, LifetimeEnv, Lifetimes, MaybeStatic};
use super::LoweringContext;
use crate::ast;
use smallvec::SmallVec;
Expand Down
33 changes: 8 additions & 25 deletions core/src/hir/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@

use super::IdentBuf;
use crate::ast;
use core::fmt::{self, Debug};
use core::fmt::Debug;
use core::hash::Hash;
use core::marker::PhantomData;

use smallvec::{smallvec, SmallVec};
use std::borrow::{Borrow, Cow};

/// Convenience const representing the number of lifetimes a [`LifetimeEnv`]
/// can hold inline before needing to dynamically allocate.
pub(crate) const INLINE_NUM_LIFETIMES: usize = 4;

/// The lifetimes and bounds found on a method or type definition (determined by
/// Kind parameter, which will be one of [`LifetimeKind`])
// TODO(Quinn): This type is going to mainly be recycled from `ast::LifetimeEnv`.
// Not fully sure how that will look like yet, but the ideas of what this will do
// is basically the same.
/// The lifetimes and bounds found on a method or type definition
#[derive(Debug)]
pub struct LifetimeEnv {
/// List of named lifetimes in scope of the method, and their bounds
Expand All @@ -32,7 +28,7 @@ pub struct LifetimeEnv {
/// `Lifetime`s will fall into this range, and we'll know that it's
/// a named lifetime if it's < `nodes.len()`, or that it's an anonymous
/// lifetime if it's < `num_lifetimes`. Otherwise, we'd have to make a
/// distinction in `Lifetime` about which kind it refers to.
/// distinction in `Lifetime` about which context it's in.
num_lifetimes: usize,
}

Expand All @@ -52,12 +48,7 @@ impl LifetimeEnv {

/// Get an iterator of all lifetimes that this must live as long as (including itself)
/// with the first lifetime always being returned first
///
/// The kind *can* be different: e.g. the Type paths in a method signature will
/// still have Lifetime<Type> even though they're in a method context.
///
/// In the medium term we may want to get rid of Type vs Method lifetimes, OR
/// make them a parameter on Type.
pub fn all_shorter_lifetimes(
&self,
lt: impl Borrow<Lifetime>,
Expand All @@ -84,7 +75,7 @@ impl LifetimeEnv {
}

pub fn all_lifetimes(&self) -> impl ExactSizeIterator<Item = Lifetime> {
(0..self.num_lifetimes()).map(|i| Lifetime::new(i))
(0..self.num_lifetimes()).map(Lifetime::new)
}

/// Returns a new [`LifetimeEnv`].
Expand Down Expand Up @@ -225,15 +216,14 @@ impl<T> MaybeStatic<T> {
}
}

/// A lifetime that exists as part of a type or method signature.
/// A lifetime that exists as part of a type name, struct signature, or method signature.
///
/// This index only makes sense in the context of a surrounding type or method; since
/// this is essentially an index into that type/method's lifetime list.
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct Lifetime(usize);

/// A set of lifetimes found on a type name or method signature (determined by
/// Kind parameter, which will be one of [`LifetimeKind`])
/// A set of lifetimes found on a type name, struct signature, or method signature
#[derive(Clone, Debug)]
pub struct Lifetimes {
indices: SmallVec<[MaybeStatic<Lifetime>; 2]>,
Expand All @@ -243,13 +233,6 @@ impl Lifetime {
pub(super) fn new(index: usize) -> Self {
Self(index)
}

/// Cast between lifetime kinds. See all_longer_lifetimes() as to why this can be necessary.
///
/// Hopefully can be removed in the long run.
pub fn cast(self) -> Self {
Lifetime::new(self.0)
}
}

impl Lifetimes {
Expand Down
4 changes: 2 additions & 2 deletions core/src/hir/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use smallvec::SmallVec;

use super::{paths, Attrs, Docs, Ident, IdentBuf, OutType, SelfType, Slice, Type, TypeContext};

use super::lifetimes::{self, Lifetime, LifetimeEnv, Lifetimes, MaybeStatic};
use super::lifetimes::{Lifetime, LifetimeEnv, Lifetimes, MaybeStatic};

/// A method exposed to Diplomat.
#[derive(Debug)]
Expand Down Expand Up @@ -142,7 +142,7 @@ impl ReturnType {
let mut add_to_set = |ty: &OutType| {
for lt in ty.lifetimes() {
if let MaybeStatic::NonStatic(lt) = lt {
set.insert(lt.cast());
set.insert(lt);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ expression: lt_to_borrowing_fields
"_s",
],
NonStatic(
diplomat_core::hir::lifetimes::Lifetime(0),
Lifetime(
0,
),
): [
"this.p_data",
],
NonStatic(
diplomat_core::hir::lifetimes::Lifetime(1),
Lifetime(
1,
),
): [
"this.q_data",
"this.inner.more_data",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ TypeContext {
lifetimes: Lifetimes {
indices: [
NonStatic(
diplomat_core::hir::lifetimes::Lifetime(0),
Lifetime(
0,
),
),
],
},
Expand Down Expand Up @@ -61,7 +63,9 @@ TypeContext {
ty: Slice(
Str(
NonStatic(
diplomat_core::hir::lifetimes::Lifetime(0),
Lifetime(
0,
),
),
UnvalidatedUtf8,
),
Expand All @@ -77,7 +81,9 @@ TypeContext {
lifetimes: Lifetimes {
indices: [
NonStatic(
diplomat_core::hir::lifetimes::Lifetime(0),
Lifetime(
0,
),
),
],
},
Expand Down Expand Up @@ -141,7 +147,9 @@ TypeContext {
ty: Slice(
Str(
NonStatic(
diplomat_core::hir::lifetimes::Lifetime(0),
Lifetime(
0,
),
),
UnvalidatedUtf8,
),
Expand Down Expand Up @@ -172,7 +180,9 @@ TypeContext {
lifetimes: Lifetimes {
indices: [
NonStatic(
diplomat_core::hir::lifetimes::Lifetime(0),
Lifetime(
0,
),
),
],
},
Expand All @@ -189,7 +199,9 @@ TypeContext {
ty: Slice(
Str(
NonStatic(
diplomat_core::hir::lifetimes::Lifetime(1),
Lifetime(
1,
),
),
UnvalidatedUtf8,
),
Expand All @@ -202,7 +214,9 @@ TypeContext {
Slice(
Str(
NonStatic(
diplomat_core::hir::lifetimes::Lifetime(1),
Lifetime(
1,
),
),
UnvalidatedUtf8,
),
Expand Down
9 changes: 3 additions & 6 deletions tool/src/dart/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,7 @@ impl<'a, 'cx> TyGenContext<'a, 'cx> {
// the type lifetimes array
for (def_lt, use_lt) in def_lifetimes.zip(use_lifetimes) {
if let MaybeStatic::NonStatic(use_lt) = use_lt {
if method_lifetime
.all_longer_lifetimes
.contains(&use_lt.cast())
{
if method_lifetime.all_longer_lifetimes.contains(&use_lt) {
let edge = format!(
"...{param_name}._fields_for_lifetime_{}()",
def.lifetimes.fmt_lifetime(def_lt)
Expand All @@ -402,7 +399,7 @@ impl<'a, 'cx> TyGenContext<'a, 'cx> {
for method_lifetime in method_lifetimes_map.values_mut() {
for lt in ty.lifetimes() {
if let MaybeStatic::NonStatic(lt) = lt {
if method_lifetime.all_longer_lifetimes.contains(&lt.cast()) {
if method_lifetime.all_longer_lifetimes.contains(&lt) {
let edge = if let hir::Type::Slice(..) = ty {
// Slices make a temporary view type that needs to be attached
// XXXManishearth: this is the wrong variable. We need to grab on to the arena
Expand Down Expand Up @@ -824,7 +821,7 @@ impl<'a, 'cx> TyGenContext<'a, 'cx> {
///
/// FIXME(Manishearth): this may need to belong in fmt.rs
fn gen_single_edge(&self, lifetime: Lifetime, lifetime_env: &LifetimeEnv) -> Cow<'static, str> {
format!("edge_{}", lifetime_env.fmt_lifetime(lifetime.cast())).into()
format!("edge_{}", lifetime_env.fmt_lifetime(lifetime)).into()
}

/// Make a list of edge arrays, one for every lifetime in a Lifetimes
Expand Down

0 comments on commit ec6b76e

Please sign in to comment.