Skip to content

Commit

Permalink
compiler: remove rustc_abi::lookup and AbiUnsupported
Browse files Browse the repository at this point in the history
These can be entirely replaced by the FromStr implementation.
  • Loading branch information
workingjubilee committed Feb 12, 2025
1 parent edff4fe commit f8570e8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 18 deletions.
9 changes: 1 addition & 8 deletions compiler/rustc_abi/src/extern_abi.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::str::FromStr;

#[cfg(feature = "nightly")]
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableOrd};
Expand Down Expand Up @@ -101,6 +100,7 @@ macro_rules! abi_impls {
}
}

#[derive(Debug)]
pub enum AbiFromStrErr {
Unknown,
}
Expand Down Expand Up @@ -214,13 +214,6 @@ impl ExternAbi {
}
}

#[derive(Copy, Clone, Debug)]
pub struct AbiUnsupported {}
/// Returns the ABI with the given name (if any).
pub fn lookup(name: &str) -> Result<Abi, AbiUnsupported> {
ExternAbi::from_str(name).map_err(|_| AbiUnsupported {})
}

pub fn all_names() -> Vec<&'static str> {
ExternAbi::ALL_VARIANTS.iter().map(|abi| abi.as_str()).collect()
}
Expand Down
9 changes: 5 additions & 4 deletions compiler/rustc_abi/src/extern_abi/tests.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
use std::assert_matches::assert_matches;
use std::str::FromStr;

use super::*;

#[allow(non_snake_case)]
#[test]
fn lookup_Rust() {
let abi = lookup("Rust");
let abi = ExternAbi::from_str("Rust");
assert!(abi.is_ok() && abi.unwrap().as_str() == "Rust");
}

#[test]
fn lookup_cdecl() {
let abi = lookup("cdecl");
let abi = ExternAbi::from_str("cdecl");
assert!(abi.is_ok() && abi.unwrap().as_str() == "cdecl");
}

#[test]
fn lookup_baz() {
let abi = lookup("baz");
assert_matches!(abi, Err(AbiUnsupported {}));
let abi = ExternAbi::from_str("baz");
assert_matches!(abi, Err(AbiFromStrErr::Unknown));
}

#[test]
Expand Down
13 changes: 8 additions & 5 deletions compiler/rustc_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ mod tests;
mod extern_abi;

pub use callconv::{Heterogeneous, HomogeneousAggregate, Reg, RegKind};
pub use extern_abi::{AbiUnsupported, ExternAbi, all_names, lookup};
pub use extern_abi::{ExternAbi, all_names};
#[cfg(feature = "nightly")]
pub use layout::{FIRST_VARIANT, FieldIdx, Layout, TyAbiInterface, TyAndLayout, VariantIdx};
pub use layout::{LayoutCalculator, LayoutCalculatorError};
Expand Down Expand Up @@ -1178,10 +1178,13 @@ impl Scalar {
#[inline]
pub fn is_bool(&self) -> bool {
use Integer::*;
matches!(self, Scalar::Initialized {
value: Primitive::Int(I8, false),
valid_range: WrappingRange { start: 0, end: 1 }
})
matches!(
self,
Scalar::Initialized {
value: Primitive::Int(I8, false),
valid_range: WrappingRange { start: 0, end: 1 }
}
)
}

/// Get the primitive representation of this type, ignoring the valid range and whether the
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1475,7 +1475,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

pub(super) fn lower_abi(&mut self, abi_str: StrLit) -> ExternAbi {
let ast::StrLit { symbol_unescaped, span, .. } = abi_str;
let extern_abi = rustc_abi::lookup(symbol_unescaped.as_str()).unwrap_or_else(|_| {
let extern_abi = symbol_unescaped.as_str().parse().unwrap_or_else(|_| {
self.error_on_invalid_abi(abi_str);
ExternAbi::Rust
});
Expand Down

0 comments on commit f8570e8

Please sign in to comment.