Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

quote_expr silently discards trailing expressions #16935

Closed
huonw opened this issue Sep 2, 2014 · 3 comments
Closed

quote_expr silently discards trailing expressions #16935

huonw opened this issue Sep 2, 2014 · 3 comments
Labels
A-syntaxext Area: Syntax extensions

Comments

@huonw
Copy link
Member

huonw commented Sep 2, 2014

// synext.rs                                                  

#![feature(plugin_registrar, quote, rustc_private)]           
#![crate_type = "dylib"]                                      

extern crate syntax;                                          
extern crate rustc;                                           

use syntax::ext::base::{MacResult, MacEager};                 
use syntax::ast;                                              
use syntax::codemap::Span;                                    
use syntax::ext::base::ExtCtxt;                               

#[plugin_registrar]                                           
pub fn registrar(reg: &mut rustc::plugin::Registry) {         
  reg.register_macro("test", expand);                         
}                                                             

fn expand(cx: &mut ExtCtxt, _sp: Span,                        
          _tts: &[ast::TokenTree]) -> Box<MacResult+'static> {
    MacEager::expr(quote_expr!(cx,                            
        std::io::println("one");                              
        std::io::println("two");                              
    ))                                                        
}                                                             
// test-synext.rs
#![feature(phase)]

#[phase(plugin)] extern crate synext;

fn main() {
    test!()
}

Compiling those with rustc synext.rs && rustc -L . test-synext.rs --pretty expanded with

#![feature(phase)]
#![feature(phase)]
#![no_std]
#![feature(globs)]
#[phase(plugin, link)]
extern crate "std" as std;
extern crate "native" as rt;

#[phase(plugin)]
extern crate synext;
#[prelude_import]
use std::prelude::*;

fn main() { std::io::println("one") }

i.e. the std::io::println("two") is gone. Wrapping the two expressions into a { ... } works.

@mrhota
Copy link
Contributor

mrhota commented Aug 19, 2016

@huonw This is an old issue, and I updated the test case a bit, but it still seems to happen:

// synext.rs
#![feature(plugin_registrar, quote, rustc_private)]
#![crate_name = "synext"]
#![crate_type = "dylib"]

extern crate syntax;
extern crate rustc_plugin;
use rustc_plugin::Registry;

use syntax::ext::base::{MacResult, MacEager};
use syntax::tokenstream;
use syntax::codemap::Span;
use syntax::ext::base::ExtCtxt;

#[plugin_registrar]
pub fn registrar(reg: &mut Registry) {
    reg.register_macro("test", expand);
}

fn expand(cx: &mut ExtCtxt, _sp: Span,
          _tts: &[tokenstream::TokenTree]) -> Box<MacResult + 'static> {
    MacEager::expr(quote_expr!(cx,
                               std::io::stdout().write("one");
                               std::io::stdout().write("two");
    ))
}
// test-synext.rs
#![feature(plugin)]
#![plugin(synext)]

fn main() {
    test!()
}

Compiling with rustup run nightly rustc synext.rs && rustup run nightly rustc -L . test-synext.rs -Z unstable-options --pretty expanded gives:

#![feature(prelude_import)]
#![no_std]
#![feature(plugin)]
#![plugin(synext)]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std as std;

fn main() { std::io::stdout().write("one") }

@mrhota
Copy link
Contributor

mrhota commented Aug 19, 2016

Also, I get an ICE if I pass b"one" and/or b"two" to the write(...) calls in synext.rs

error: internal compiler error: unexpected panic

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

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: run with `RUST_BACKTRACE=1` for a backtrace

thread 'rustc' panicked at 'unhandled token in quote!', ../src/libsyntax/ext/quote.rs:707
stack backtrace:
   1:        0x111037e79 - std::sys::backtrace::tracing::imp::write::h482d45d91246faa2
   2:        0x111046b70 - std::panicking::default_hook::_{{closure}}::h89158f66286b674e
   3:        0x111044e9f - std::panicking::default_hook::h9e30d428ee3b0c43
   4:        0x1110455a6 - std::panicking::rust_panic_with_hook::h2224f33fb7bf2f4c
   5:        0x1107fd4d4 - std::panicking::begin_panic::h6da27a7ee15843ce
   6:        0x1109e95f3 - syntax::ext::quote::expr_mk_token::h7c3422b80bef3bd5
   7:        0x1109ebdb0 - syntax::ext::quote::statements_mk_tt::he0f33941b94e20bb
   8:        0x1108203af - _<core..iter..FlatMap<I, U, F> as core..iter..iterator..Iterator>::next::hb7943eed4d804bfc
   9:        0x11081918f - _<core..iter..Chain<A, B> as core..iter..iterator..Iterator>::next::hf66dec9ac7ebfd3e
  10:        0x11081e49a - _<collections..vec..Vec<T> as core..iter..traits..FromIterator<T>>::from_iter::hb6ba9a4116122593
  11:        0x1109ea625 - syntax::ext::quote::statements_mk_tt::he0f33941b94e20bb
  12:        0x1109ed8e2 - syntax::ext::quote::statements_mk_tts::hc942124e7cb85dbc
  13:        0x1109eda54 - syntax::ext::quote::expand_tts::h25510a66e2e03c00
  14:        0x1109eefa4 - syntax::ext::quote::expand_parse_call::h362dceaee0d8b0c6
  15:        0x1109e4c6a - syntax::ext::quote::expand_quote_expr::h7f4c2771cabcf421
  16:        0x1109b07d5 - _<F as syntax..ext..base..TTMacroExpander>::expand::h2cbfb95c85d07886
  17:        0x1109cb75b - syntax::ext::expand::expand_mac_invoc::mac_result::ha3a8dfd05d9eecb6
  18:        0x11096d850 - _<syntax..ptr..P<T>>::and_then::h53ce1d732b16c30f
  19:        0x11088da64 - syntax::fold::Folder::fold_exprs::haec82b8e9fe75416
  20:        0x1108f0234 - syntax::fold::noop_fold_expr::hab84161d928c2074
  21:        0x1109c6d85 - syntax::ext::expand::expand_expr::hd4de34daa0a5a90a
  22:        0x11096d963 - _<syntax..ptr..P<T>>::and_then::h53ce1d732b16c30f
  23:        0x1109cd0de - syntax::ext::expand::expand_stmt::hf1246bdbc447b35b
  24:        0x11083d0c2 - _<collections..vec..Vec<T> as syntax..util..move_map..MoveMap<T>>::move_flat_map::h95ab7180e0970e65
  25:        0x1109dc77f - _<syntax..ext..expand..MacroExpander<'a, 'b> as syntax..fold..Folder>::fold_block::h84ca09e1246a6f7f
  26:        0x1108b2c3f - syntax::fold::noop_fold_item_kind::he96b2686f10d5f9d
  27:        0x1108bee78 - syntax::fold::noop_fold_item_simple::hb58741bcf641e653
  28:        0x1109d0d40 - syntax::ext::expand::expand_annotatable::hd731ac0c7a5098e1
  29:        0x1109cc7cf - syntax::ext::expand::expand_item::h34a68666bcb77b42
  30:        0x1109dc20a - _<syntax..ext..expand..MacroExpander<'a, 'b> as syntax..fold..Folder>::fold_item::h11dc20d6fdb44d60
  31:        0x1108bd942 - syntax::fold::noop_fold_mod::h7ccd83e899e81c3c
  32:        0x1108b26b2 - syntax::fold::noop_fold_item_kind::he96b2686f10d5f9d
  33:        0x1108bee78 - syntax::fold::noop_fold_item_simple::hb58741bcf641e653
  34:        0x1109d102b - syntax::ext::expand::expand_annotatable::hd731ac0c7a5098e1
  35:        0x1109cc7cf - syntax::ext::expand::expand_item::h34a68666bcb77b42
  36:        0x1109dc342 - _<syntax..ext..expand..MacroExpander<'a, 'b> as syntax..fold..Folder>::fold_item::h11dc20d6fdb44d60
  37:        0x1109db30e - _<syntax..ext..expand..MacroExpander<'a, 'b> as syntax..fold..Folder>::fold_crate::hb0a12b0d8df69afd
  38:        0x1109dda1e - syntax::ext::expand::expand_crate_with_expander::h77575f1d1b34ae40
  39:        0x1109dd4ba - syntax::ext::expand::expand_crate::h72ec18c6565094c4
  40:        0x10c3d668b - rustc_driver::driver::phase_2_configure_and_expand::_{{closure}}::hae7f286174f41285
  41:        0x10c389133 - rustc_driver::driver::phase_2_configure_and_expand::h7a450d24678fc450
  42:        0x10c38089c - rustc_driver::driver::compile_input::hd9ecc57abd3cba85
  43:        0x10c3b2579 - rustc_driver::run_compiler::h184264500271cc39
  44:        0x10c2fdc94 - std::panicking::try::do_call::h17a7a17ad7240c5c
  45:        0x11104d8aa - __rust_maybe_catch_panic
  46:        0x10c317d57 - _<F as alloc..boxed..FnBox<A>>::call_box::h93f9128277b2964a
  47:        0x111043c65 - std::sys::thread::Thread::new::thread_start::he0bf102845911132
  48:     0x7fff89ecf99c - _pthread_body
  49:     0x7fff89ecf919 - _pthread_start

@Mark-Simulacrum
Copy link
Member

Closing in favor of #12266.

bors added a commit to rust-lang-ci/rust that referenced this issue Mar 31, 2024
Handle panicking like rustc CTFE does

Instead of using `core::fmt::format` to format panic messages, which may in turn panic too and cause recursive panics and other messy things, redirect `panic_fmt` to `const_panic_fmt` like CTFE, which in turn goes to `panic_display` and does the things normally. See the tests for the full call stack.

The tests don't work yet, I probably missed something in minicore.

fixes rust-lang#16907 in my local testing, I also need to add a test for it
lnicola pushed a commit to lnicola/rust that referenced this issue Apr 24, 2024
Implement `BeginPanic` handling in const eval

for rust-lang#16935, needs some figuring out of how to write these tests correctly
RalfJung pushed a commit to RalfJung/rust that referenced this issue Apr 27, 2024
Implement `BeginPanic` handling in const eval

for rust-lang#16935, needs some figuring out of how to write these tests correctly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-syntaxext Area: Syntax extensions
Projects
None yet
Development

No branches or pull requests

3 participants