Skip to content
Merged
Show file tree
Hide file tree
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
21 changes: 21 additions & 0 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,27 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
{
rustc_parse::fake_token_stream_for_item(&self.cx.sess.psess, item_inner)
}
Annotatable::Item(item_inner) if item_inner.tokens.is_none() => {
rustc_parse::fake_token_stream_for_item(&self.cx.sess.psess, item_inner)
}
// When a function has EII implementations attached (via `eii_impls`),
// use fake tokens so the pretty-printer re-emits the EII attribute
// (e.g. `#[hello]`) in the token stream. Without this, the EII
// attribute is lost during the token roundtrip performed by
// `AttrProcMacro` expanders like `contracts::requires/ensures`,
// breaking the EII link on the resulting re-parsed item.
Annotatable::Item(item_inner)
if matches!(&item_inner.kind,
ItemKind::Fn(f) if !f.eii_impls.is_empty()) =>
{
rustc_parse::fake_token_stream_for_item(&self.cx.sess.psess, item_inner)
}
Annotatable::ForeignItem(item_inner) if item_inner.tokens.is_none() => {
rustc_parse::fake_token_stream_for_foreign_item(
&self.cx.sess.psess,
item_inner,
)
}
_ => item.to_tokens(),
};
let attr_item = attr.get_normal_item();
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_parse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,15 @@ pub fn fake_token_stream_for_item(psess: &ParseSess, item: &ast::Item) -> TokenS
unwrap_or_emit_fatal(source_str_to_stream(psess, filename, source, Some(item.span)))
}

pub fn fake_token_stream_for_foreign_item(
psess: &ParseSess,
item: &ast::ForeignItem,
) -> TokenStream {
let source = pprust::foreign_item_to_string(item);
let filename = FileName::macro_expansion_source_code(&source);
unwrap_or_emit_fatal(source_str_to_stream(psess, filename, source, Some(item.span)))
}

pub fn fake_token_stream_for_crate(psess: &ParseSess, krate: &ast::Crate) -> TokenStream {
let source = pprust::crate_to_string_for_macros(krate);
let filename = FileName::macro_expansion_source_code(&source);
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/eii/eii_impl_with_contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//@ run-pass
//@ ignore-backends: gcc
//@ ignore-windows

#![feature(extern_item_impls)]
#![feature(contracts)]
#![allow(incomplete_features)]

#[eii(hello)]
fn hello(x: u64);

#[hello]
#[core::contracts::requires(x > 0)]
fn hello_impl(x: u64) {
println!("{x:?}")
}

fn main() {
hello(42);
}
11 changes: 11 additions & 0 deletions tests/ui/eii/ice_contract_attr_on_eii_generated_item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//@ compile-flags: --crate-type rlib

#![feature(extern_item_impls)]
#![feature(contracts)]
#![allow(incomplete_features)]

#[eii]
#[core::contracts::ensures]
//~^ ERROR contract annotations is only supported in functions with bodies
//~| ERROR contract annotations can only be used on functions
fn implementation();
14 changes: 14 additions & 0 deletions tests/ui/eii/ice_contract_attr_on_eii_generated_item.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: contract annotations is only supported in functions with bodies
--> $DIR/ice_contract_attr_on_eii_generated_item.rs:8:1
|
LL | #[core::contracts::ensures]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: contract annotations can only be used on functions
--> $DIR/ice_contract_attr_on_eii_generated_item.rs:8:1
|
LL | #[core::contracts::ensures]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

Loading