Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test case where a type and a value have the same name. (#60) #61

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/indexed_crate.rs
Original file line number Diff line number Diff line change
@@ -1060,5 +1060,65 @@ mod tests {

assert_exported_items_match(test_crate, &expected_items);
}

#[test]
fn type_and_value_with_matching_names() {
let test_crate = "type_and_value_with_matching_names";
let expected_items = btreemap! {
"Foo" => btreeset![
"type_and_value_with_matching_names::Foo",
"type_and_value_with_matching_names::nested::Foo",
],
"Bar" => btreeset![
"type_and_value_with_matching_names::Bar",
"type_and_value_with_matching_names::nested::Bar",
],
};

let rustdoc = load_pregenerated_rustdoc(test_crate);
let indexed_crate = IndexedCrate::new(&rustdoc);

// We can't use the `assert_exported_items_match()` helper function,
// since it assumes that names are unique.
// This test case *specifically* tests the situation where a type and a value
// happen to have the same name.
//
// Instead, we implement the needed test logic directly.
for (&expected_item_name, expected_importable_paths) in &expected_items {
assert!(
!expected_item_name.contains(':'),
"only direct item names can be checked at the moment: {expected_item_name}"
);

let item_id_candidates = rustdoc
.index
.iter()
.filter_map(|(id, item)| {
(item.name.as_deref() == Some(expected_item_name)).then_some(id)
})
.collect_vec();
if item_id_candidates.len() != 2 {
panic!(
"Expected to find exactly two items with name {expected_item_name}, \
but found these matching IDs: {item_id_candidates:?}"
);
}
for item_id in item_id_candidates {
let actual_items: Vec<_> = indexed_crate
.publicly_importable_names(item_id)
.into_iter()
.map(|components| components.into_iter().join("::"))
.collect();
let deduplicated_actual_items: BTreeSet<_> =
actual_items.iter().map(|x| x.as_str()).collect();
assert_eq!(
actual_items.len(),
deduplicated_actual_items.len(),
"duplicates found: {actual_items:?}"
);
assert_eq!(expected_importable_paths, &deduplicated_actual_items);
}
}
}
}
}
9 changes: 9 additions & 0 deletions test_crates/type_and_value_with_matching_names/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
publish = false
name = "type_and_value_with_matching_names"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
24 changes: 24 additions & 0 deletions test_crates/type_and_value_with_matching_names/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//! In Rust, type names and value names (including both `fn` and `const`) have different,
//! mutually-disjoint namespaces. It's allowed for those namespaces to have matching names.
//! When the name is used, the surrounding context determines whether it's resolved
//! to the value or to the type by that name.
//!
//! This package exports the following:
//! - the function `Foo`, also as `nested::Foo`
//! - the type `Foo`, also as `nested::Foo`
//! - the const value `Bar`, also as `nested::Bar`
//! - the type `Bar`, also as `nested::Bar`

pub mod nested {
pub struct Foo {}

#[allow(non_snake_case)]
pub fn Foo() {}

pub struct Bar {}

#[allow(non_upper_case_globals)]
pub const Bar: Bar = Bar {};
}

pub use nested::*;