Skip to content

False circular module error with #[path], inner attribute, and nested mod.rs #162080

Description

@joseluis

A module loaded with #[path] is incorrectly reported as circular when the loaded module contains an inner attribute and declares a nested module using the name/mod.rs layout.

I tried this code:

Cargo.toml
main.rs
name/
  mod.rs
  name/
    mod.rs
# Cargo.toml

[package]
name = "circular_modules"
version = "0.0.0"

[[bin]]
name = "main"
path = "main.rs"
// main.rs

#[path = "name/mod.rs"]
mod name;

fn main() {}
// name/mod.rs

#![allow(unused)]

mod name;
// name/name/mod.rs

I expected to see this happen: the crate compiles successfully. The modules are distinct: main.rs loads name/mod.rs, which then loads name/name/mod.rs.

Instead, this happened:

error: circular modules: name/name/mod.rs -> name/name/mod.rs
 --> name/mod.rs:3:1
  |
3 | mod name;
  | ^^^^^^^^^

The issue appears to depend on this particular combination.

Removing the inner attribute makes it compile:

// name/mod.rs

mod name;

Removing the parent's #[path] attribute also makes it compile:

// main.rs

mod name;

fn main() {}

Keeping the parent's #[path] and the inner attribute, but changing the child module layout from:

name/name/mod.rs

to:

name/name.rs

also makes it compile.

So the false circular-module diagnostic appears when:

  • the parent module is loaded through #[path];
  • that loaded module contains an inner attribute;
  • its child is resolved using the name/mod.rs layout.

Meta

The bug occurs on both stable 1.98.0 and current nightly.

rustc +stable --version --verbose:

rustc 1.98.0 (88d9e12ae 2026-08-18)
binary: rustc
commit-hash: 88d9e12ae178fab0fb5cc050a94da85685d449ea
commit-date: 2026-08-18
host: x86_64-unknown-linux-gnu
release: 1.98.0
LLVM version: 22.1.8

rustc +nightly --version --verbose:

rustc 1.100.0-nightly (908501772 2026-08-30)
binary: rustc
commit-hash: 90850177249efe0321573c569aec5d12b257f8d6
commit-date: 2026-08-30
host: x86_64-unknown-linux-gnu
release: 1.100.0-nightly
LLVM version: 23.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)C-bugCategory: This is a bug.S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions