Skip to content

Commit

Permalink
Merge pull request #988 from candy-lang/983-extension-panic-on-gettin…
Browse files Browse the repository at this point in the history
…g-parent-of-root

Fix `id_path.parent()` for top-level IDs
  • Loading branch information
jwbot authored Mar 14, 2024
2 parents 60f66ed + 1cc3fb6 commit 25cd24b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ members = [
[workspace.package]
edition = "2021"
rust-version = "1.78.0"

[profile.release]
# This adds file and line number information to backtraces while only increasing
# the executable's file size from 15 MB to 76 MB (compared to around 170 MB with
# full debug information).
debug = "limited"
18 changes: 14 additions & 4 deletions compiler/frontend/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,14 @@ pub enum IdKey {
Positional(usize),
}
impl IdPath {
fn is_empty(&self) -> bool {
#[must_use]
fn is_root(&self) -> bool {
self.0.is_empty()
}

#[must_use]
fn child(&self, key: IdKey) -> Self {
if self.is_empty() {
if self.is_root() {
Self::from(key)
} else {
Self(format!("{}:{key}", self.0))
Expand All @@ -164,7 +165,16 @@ impl IdPath {

#[must_use]
fn parent(&self) -> Option<Self> {
self.0.rfind(':').map(|i| Self(self.0[..i].to_string()))
self.0
.rfind(':')
.map(|i| Self(self.0[..i].to_string()))
.or_else(|| {
if self.is_root() {
None
} else {
Some(Self::default())
}
})
}
}
impl From<IdKey> for IdPath {
Expand Down Expand Up @@ -255,7 +265,7 @@ impl Id {

#[must_use]
pub fn is_root(&self) -> bool {
self.keys.is_empty()
self.keys.is_root()
}

#[must_use]
Expand Down

0 comments on commit 25cd24b

Please sign in to comment.