Skip to content

ICE: None in rustc_parse/src/parser/attr_wrapper.rs #137509

Open
@cushionbadak

Description

@cushionbadak

Code

#![feature(contracts)]

struct Dummy(usize);

impl Dummy {
    #[core::contracts::ensures(true)]
    fn new(_v: usize) -> Dummy {
        Dummy(loop)
    }
}
Original Code (Mutant from Fuzzing)

//! Checks for compilation errors related to adding contracts to non-function items.

#![feature(contracts)]
//~^ WARN the feature `contracts` is incomplete and may not be safe to use and/or cause compiler crashes [incomplete_features]
#![allow(dead_code)]

#[core::contracts::requires(true)]
//~^ ERROR contract annotations can only be used on functions
struct Dummy(usize);

#[core::contracts::ensures(|v| v == 100)]
//~^ ERROR contract annotations can only be used on functions
const MAX_VAL: usize = 100;

// FIXME: Improve the error message here. The macro thinks this is a function.
#[core::contracts::ensures(|v| v == 100)]
//~^ ERROR contract annotations is only supported in functions with bodies
type NewDummy = fn(usize) -> Dummy;

#[reexports::foo_local(|v| v == 100)]
//~^ ERROR contract annotations is only supported in functions with bodies
const NEW_DUMMY_FN : fn(usize) -> Dummy = || { Dummy(0xc0000000) };

#[core::contracts::requires(true)]
//~^ ERROR contract annotations can only be used on functions
impl Dummy {

    // This should work
    #[core::contracts::ensures(|ret| ret.0 == v)]
    fn new(v: usize) -> Dummy {
        Dummy(loop)
    }
}

#[core::contracts::ensures(|dummy| dummy.0 > 0)]
//~^ ERROR contract annotations can only be used on functions
impl From<usize> for Dummy {
    // This should work.
    #[core::contracts::ensures(|ret| ret.0 == v)]
    fn from(value: usize) -> Self {
        Dummy::new(value)
    }
}

/// You should not be able to annotate a trait either.
#[core::contracts::requires(true)]
//~^ ERROR contract annotations can only be used on functions
pub trait DummyBuilder {
    fn build() -> Dummy;
}

fn main() {
}


// /root/workspace/seeds_250219/tests/ui/contracts/disallow-contract-annotation-on-non-fn.rs

Meta

rustc --version --verbose:

rustc 1.87.0-nightly (f8a913b13 2025-02-23)
binary: rustc
commit-hash: f8a913b1381e90379c7ca63ac2b88b9518936628
commit-date: 2025-02-23
host: x86_64-apple-darwin
release: 1.87.0-nightly
LLVM version: 20.1.0

Error output

error: expected `{`, found `)`
 --> 8484.rs:8:19
  |
8 |         Dummy(loop)
  |               ----^ expected `{`
  |               |
  |               while parsing this `loop` expression

error: non-item in item list
  --> 8484.rs:10:1
   |
5  | impl Dummy {
   |            - item list starts here
...
10 | }
   | ^
   | |
   | non-item starts here
   | item list ends here


thread 'rustc' panicked at compiler/rustc_parse/src/parser/attr_wrapper.rs:511:80:
called `Option::unwrap()` on a `None` value
Backtrace

thread 'rustc' panicked at compiler/rustc_parse/src/parser/attr_wrapper.rs:511:80:
called `Option::unwrap()` on a `None` value
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic
   3: core::option::unwrap_failed
   4: <rustc_parse::parser::attr_wrapper::LazyAttrTokenStreamImpl as rustc_ast::tokenstream::ToAttrTokenStream>::to_attr_token_stream
   5: rustc_ast::tokenstream::attrs_and_tokens_to_token_trees
   6: <rustc_expand::base::Annotatable>::to_tokens
   7: <rustc_expand::expand::MacroExpander>::expand_invoc
   8: <rustc_expand::expand::MacroExpander>::fully_expand_fragment
   9: <rustc_expand::expand::MacroExpander>::expand_crate
  10: rustc_interface::passes::resolver_for_lowering_raw
      [... omitted 2 frames ...]
  11: rustc_interface::passes::create_and_enter_global_ctxt::<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure#2}>
  12: rustc_interface::interface::run_compiler::<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

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

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: please make sure that you have updated to the latest nightly

note: please attach the file at `/Volumes/T7/workspace/250224 scratch/codes/rustc-ice-2025-02-24T05_29_30-81273.txt` to your bug report

query stack during panic:
#0 [resolver_for_lowering_raw] getting the resolver for lowering
end of query stack
error: aborting due to 2 previous errors

Note

  • ICE location: compiler/rustc_parse/src/parser/attr_wrapper.rs Line-511
    for flat_token in iter {
    match flat_token {
    FlatToken::Token((Token { kind: TokenKind::OpenDelim(delim), span }, spacing)) => {
    stack_rest.push(mem::replace(
    &mut stack_top,
    FrameData { open_delim_sp: Some((delim, span, spacing)), inner: vec![] },
    ));
    }
    FlatToken::Token((Token { kind: TokenKind::CloseDelim(delim), span }, spacing)) => {
    let frame_data = mem::replace(&mut stack_top, stack_rest.pop().unwrap());
    let (open_delim, open_sp, open_spacing) = frame_data.open_delim_sp.unwrap();

@rustbot label +F-contracts

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.F-contracts`#![feature(contracts)]`I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️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