Skip to content

No index for a field #50493

Closed
Closed
@dtolnay

Description

@dtolnay

This is a strange one to summarize. As best as I have been able to tell: the compiler ICEs with "no index for a field" if two derived functions each independently acknowledge the existence of a field that is not visible to them.

In any case, here is as far as I got minimizing this one:

#!/bin/sh

cargo new --lib repro
cargo new --lib repro_derive

echo >repro/src/lib.rs '
#[macro_use]
extern crate repro_derive;

#[derive(Derive)]
struct Restricted {
    pub(in restricted) field: usize,
}

mod restricted {}

fn main() {}
'

echo >>repro/Cargo.toml '
repro_derive = { path = "../repro_derive" }
'

echo >repro_derive/src/lib.rs '
extern crate proc_macro;
use proc_macro::TokenStream;

#[proc_macro_derive(Derive)]
pub fn derive(_: TokenStream) -> TokenStream {
    let code = "
        fn one(r: Restricted) {
            r.field;
        }
        fn two(r: Restricted) {
            r.field;
        }
    ";
    
    code.parse().unwrap()
}
'

echo >>repro_derive/Cargo.toml '
[lib]
proc-macro = true
'

cargo build --manifest-path repro/Cargo.toml

Reasonable error message on stable and beta. But on nightly:

error: visibilities can only be restricted to ancestor modules
 --> src/lib.rs:7:12
  |
7 |     pub(in restricted) field: usize,
  |            ^^^^^^^^^^

error[E0616]: field `field` of struct `Restricted` is private
 --> src/lib.rs:5:10
  |
5 | #[derive(Derive)]
  |          ^^^^^^

thread 'main' panicked at 'no index for a field', libcore/option.rs:914:5
note: Run with `RUST_BACKTRACE=1` for a backtrace.
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0616`.

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

Mentioning @petrochenkov who added the panicking function in #49718.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-betaPerformance or correctness regression from stable to beta.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions