about summary refs log tree commit diff
path: root/src/librustdoc/doctest/make.rs
diff options
context:
space:
mode:
authorNicholas Nethercote <n.nethercote@gmail.com>2025-03-21 09:47:43 +1100
committerNicholas Nethercote <n.nethercote@gmail.com>2025-04-01 14:08:57 +1100
commitdf247968f267d30fb8b048c21f595f2293d8ff62 (patch)
tree5228ba95557a3faf0c3133b276fd408e7e3863b4 /src/librustdoc/doctest/make.rs
parent43018eacb61da96b718f70b7719bf5e51207df61 (diff)
downloadrust-df247968f267d30fb8b048c21f595f2293d8ff62.tar.gz
rust-df247968f267d30fb8b048c21f595f2293d8ff62.zip
Move `ast::Item::ident` into `ast::ItemKind`.
`ast::Item` has an `ident` field.

- It's always non-empty for these item kinds: `ExternCrate`, `Static`,
  `Const`, `Fn`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`,
  `Trait`, `TraitAlias`, `MacroDef`, `Delegation`.

- It's always empty for these item kinds: `Use`, `ForeignMod`,
  `GlobalAsm`, `Impl`, `MacCall`, `DelegationMac`.

There is a similar story for `AssocItemKind` and `ForeignItemKind`.

Some sites that handle items check for an empty ident, some don't. This
is a very C-like way of doing things, but this is Rust, we have sum
types, we can do this properly and never forget to check for the
exceptional case and never YOLO possibly empty identifiers (or possibly
dummy spans) around and hope that things will work out.

The commit is large but it's mostly obvious plumbing work. Some notable
things.

- `ast::Item` got 8 bytes bigger. This could be avoided by boxing the
  fields within some of the `ast::ItemKind` variants (specifically:
  `Struct`, `Union`, `Enum`). I might do that in a follow-up; this
  commit is big enough already.

- For the visitors: `FnKind` no longer needs an `ident` field because
  the `Fn` within how has one.

- In the parser, the `ItemInfo` typedef is no longer needed. It was used
  in various places to return an `Ident` alongside an `ItemKind`, but
  now the `Ident` (if present) is within the `ItemKind`.

- In a few places I renamed identifier variables called `name` (or
  `foo_name`) as `ident` (or `foo_ident`), to better match the type, and
  because `name` is normally used for `Symbol`s. It's confusing to see
  something like `foo_name.name`.
Diffstat (limited to 'src/librustdoc/doctest/make.rs')
-rw-r--r--src/librustdoc/doctest/make.rs8
1 files changed, 4 insertions, 4 deletions
diff --git a/src/librustdoc/doctest/make.rs b/src/librustdoc/doctest/make.rs
index 56b1e76ae8c..4edd5433de6 100644
--- a/src/librustdoc/doctest/make.rs
+++ b/src/librustdoc/doctest/make.rs
@@ -350,21 +350,21 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
             info.has_global_allocator = true;
         }
         match item.kind {
-            ast::ItemKind::Fn(_) if !info.has_main_fn => {
+            ast::ItemKind::Fn(ref fn_item) if !info.has_main_fn => {
                 // We only push if it's the top item because otherwise, we would duplicate
                 // its content since the top-level item was already added.
-                if item.ident.name == sym::main {
+                if fn_item.ident.name == sym::main {
                     info.has_main_fn = true;
                 }
             }
-            ast::ItemKind::ExternCrate(original) => {
+            ast::ItemKind::ExternCrate(original, ident) => {
                 is_extern_crate = true;
                 if !info.already_has_extern_crate
                     && let Some(crate_name) = crate_name
                 {
                     info.already_has_extern_crate = match original {
                         Some(name) => name.as_str() == *crate_name,
-                        None => item.ident.as_str() == *crate_name,
+                        None => ident.as_str() == *crate_name,
                     };
                 }
             }