Rust-GCC / Rust-GCC/gccrs

segfault in Rust::AST::SingleASTNode::SingleASTNode / Type::clone_type()

Open
#3,968 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C++
Stars
2.9k
Forks
230
Avg merge
19h 55m
Merged PRs (30d)
67

Description

auto-reduced (treereduce-rust):

//@compile-flags: -frust-incomplete-and-experimental-compiler-do-not-use
//

//

struct X;

macro_rules! tt_trait {
    ($id:ident) => { & $arg tt_trait }
}

fn main() {
    //

    fn f_tt(x: &X) -> tt_trait!(dyn) {
        x
    }

    let x = X;
}

original code

original:

// Under the 2015 edition with the keyword_idents lint, `dyn` is
// not entirely acceptable as an identifier.
//
// We currently do not attempt to detect or fix uses of `dyn` as an
// identifier under a macro.
//
//@ check-pass
//@ edition:2015

#![allow(non_camel_case_types)]
#![deny(keyword_idents)]

mod outer_mod {
    pub mod r#dyn {
        pub struct r#dyn;
    }
}

// Here we are illustrating that the current lint does not flag the
// occurrences of `dyn` in this macro definition; however, it
// certainly *could* (and it would be nice if it did), since these
// occurrences are not compatible with the 2018 edition's
// interpretation of `dyn` as a keyword.
macro_rules! defn_has_dyn_idents {
    () => { ::outer_mod::dyn::dyn }
}

struct X;
trait Trait { fn defn_has_dyn_idents(&self) { }}
impl Trait for X { }

macro_rules! tt_trait {
    ($id:ident) => { & $arg tt_trait }
}

macro_rules! id_trait {
    ($id:ident) => { & $id Trait }
}

fn main() {
    defn_has_dyn_idents!();

    // Here we are illustrating that the current lint does not flag
    // the occurrences of `dyn` in these macro invocations. It
    // definitely should *not* flag the one in `tt_trait`, since that
    // is expanding in a valid fashion to `&dyn Trait`.
    //
    // It is arguable whether it would be valid to flag the occurrence
    // in `id_trait`, since that macro specifies that it takes an
    // `ident` as its input.
    fn f_tt(x: &X) -> tt_trait!(dyn) { x }
    fn f_id(x: &X) -> id_trait!(dyn) { x }

    let x = X;
    f_tt(&x).hello();
    f_id(&x).hello();
}

Version information:

5dc8c4d20d62462ebce137c6e5146235ab9e9e2e

Command:
/home/matthias/vcs/github/gccrs/gccrs-build/gcc/crab1 -frust-incomplete-and-experimental-compiler-do-not-use

Program output

/tmp/icemaker_global_tempdir.ypCSWBUKJ5AY/rustc_testrunner_tmpdir_reporting.tSpefpM5HnNA/mvce.rs:8:24: error: failed to parse path as first component of type no bounds
    8 |     ($id:ident) => { & $arg tt_trait }
      |                        ^
/tmp/icemaker_global_tempdir.ypCSWBUKJ5AY/rustc_testrunner_tmpdir_reporting.tSpefpM5HnNA/mvce.rs:8:24: error: failed to parse referenced type in reference type
crab1: internal compiler error: Segmentation fault
0x2fecaf8 internal_error(char const*, ...)
	../../gcc/diagnostic-global-context.cc:517
0x196d57f crash_signal
	../../gcc/toplev.cc:321
0xcab3f4 Rust::AST::Type::clone_type() const
	../../gcc/rust/ast/rust-ast.h:1512
0xcab3f4 Rust::AST::SingleASTNode::SingleASTNode(Rust::AST::SingleASTNode const&)
	../../gcc/rust/ast/rust-ast.cc:71
0xddb181 void std::_Construct<Rust::AST::SingleASTNode, Rust::AST::SingleASTNode const&>(Rust::AST::SingleASTNode*, Rust::AST::SingleASTNode const&)
	/usr/include/c++/15.1.1/bits/stl_construct.h:133
0xddb181 Rust::AST::SingleASTNode* std::__do_uninit_copy<Rust::AST::SingleASTNode const*, Rust::AST::SingleASTNode const*, Rust::AST::SingleASTNode*>(Rust::AST::SingleASTNode const*, Rust::AST::SingleASTNode const*, Rust::AST::SingleASTNode*)
	/usr/include/c++/15.1.1/bits/stl_uninitialized.h:145
0xddb181 Rust::AST::SingleASTNode* std::uninitialized_copy<Rust::AST::SingleASTNode const*, Rust::AST::SingleASTNode*>(Rust::AST::SingleASTNode const*, Rust::AST::SingleASTNode const*, Rust::AST::SingleASTNode*)
	/usr/include/c++/15.1.1/bits/stl_uninitialized.h:266
0xddb181 Rust::AST::SingleASTNode* std::__uninitialized_copy_a<Rust::AST::SingleASTNode const*, Rust::AST::SingleASTNode const*, Rust::AST::SingleASTNode*, Rust::AST::SingleASTNode>(Rust::AST::SingleASTNode const*, Rust::AST::SingleASTNode const*, Rust::AST::SingleASTNode*, std::allocator<Rust::AST::SingleASTNode>&)
	/usr/include/c++/15.1.1/bits/stl_uninitialized.h:637
0xddb181 void std::vector<Rust::AST::SingleASTNode, std::allocator<Rust::AST::SingleASTNode> >::_M_range_initialize_n<Rust::AST::SingleASTNode const*, Rust::AST::SingleASTNode const*>(Rust::AST::SingleASTNode const*, Rust::AST::SingleASTNode const*, unsigned long)
	/usr/include/c++/15.1.1/bits/stl_vector.h:1988
0xddb181 std::vector<Rust::AST::SingleASTNode, std::allocator<Rust::AST::SingleASTNode> >::vector(std::initializer_list<Rust::AST::SingleASTNode>, std::allocator<Rust::AST::SingleASTNode> const&)
	/usr/include/c++/15.1.1/bits/stl_vector.h:712
0xddb181 transcribe_type
	../../gcc/rust/expand/rust-macro-expand.cc:978
0xddb181 transcribe_context
	../../gcc/rust/expand/rust-macro-expand.cc:1020
0xddb181 Rust::MacroExpander::transcribe_rule(Rust::AST::MacroRulesDefinition&, Rust::AST::MacroRule&, Rust::AST::DelimTokenTree&, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Rust::MatchedFragmentContainer*, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, Rust::MatchedFragmentContainer*> > >&, Rust::AST::InvocKind, Rust::MacroExpander::ContextType)
	../../gcc/rust/expand/rust-macro-expand.cc:1106
0xdde7f3 Rust::MacroExpander::expand_decl_macro(unsigned long, Rust::AST::MacroInvocData&, Rust::AST::MacroRulesDefinition&, Rust::AST::InvocKind)
	../../gcc/rust/expand/rust-macro-expand.cc:127
0xddedf5 Rust::MacroExpander::expand_invoc(Rust::AST::MacroInvocation&, Rust::AST::InvocKind)
	../../gcc/rust/expand/rust-macro-expand.cc:309
0xe9bb19 Rust::ExpandVisitor::maybe_expand_type(std::unique_ptr<Rust::AST::Type, std::default_delete<Rust::AST::Type> >&)
	../../gcc/rust/expand/rust-expand-visitor.cc:353
0xe9cc30 Rust::ExpandVisitor::visit(Rust::AST::Function&)
	../../gcc/rust/expand/rust-expand-visitor.cc:765
0xea10db void Rust::ExpandVisitor::expand_macro_children<std::vector<std::unique_ptr<Rust::AST::Stmt, std::default_delete<Rust::AST::Stmt> >, std::allocator<std::unique_ptr<Rust::AST::Stmt, std::default_delete<Rust::AST::Stmt> > > >, std::unique_ptr<Rust::AST::Stmt, std::default_delete<Rust::AST::Stmt> > >(std::vector<std::unique_ptr<Rust::AST::Stmt, std::default_delete<Rust::AST::Stmt> >, std::allocator<std::unique_ptr<Rust::AST::Stmt, std::default_delete<Rust::AST::Stmt> > > >&, std::function<std::unique_ptr<Rust::AST::Stmt, std::default_delete<Rust::AST::Stmt> > (Rust::AST::SingleASTNode)>)
	../../gcc/rust/expand/rust-expand-visitor.h:131
0xea10db Rust::ExpandVisitor::expand_inner_stmts(Rust::AST::BlockExpr&)
	../../gcc/rust/expand/rust-expand-visitor.cc:330
0xea2290 Rust::ExpandVisitor::visit(Rust::AST::BlockExpr&)
	../../gcc/rust/expand/rust-expand-visitor.cc:630
Please submit a full bug report, with preprocessed source (by using -freport-bug).
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the crash with the reduced Rust snippet and the compiler flag shown in the issue. Start in gcc/rust/expand/rust-macro-expand.cc around transcribe_type and inspect the Rust::AST::Type::clone_type path in gcc/rust/ast/rust-ast.h. Done means the input produces diagnostics without a segmentation fault.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.