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

*&X allows referring to the static X by value as if X were a const #26121

Closed
oli-obk opened this issue Jun 9, 2015 · 2 comments
Closed

*&X allows referring to the static X by value as if X were a const #26121

oli-obk opened this issue Jun 9, 2015 · 2 comments
Labels
A-typesystem Area: The type system

Comments

@oli-obk
Copy link
Contributor

oli-obk commented Jun 9, 2015

Found by @eddyb at #25570 (comment).

fn main() {
    static A: i32 = 0;
    static B: i32 = *&A;
    println!("{:?}", B);
}

aborts the compiler:

Illegal instruction (core dumped)
playpen: application terminated with error code 132

static A: i32 = 0;
static B: i32 = *&A;

fn main() {
    println!("{:?}", B);
}

prints 0 on the terminal without any warnings or errors

It doesn't really cause any weird behavior if it's a global static, it just acts as if the referred to static were a const: PlayPen

@oli-obk
Copy link
Contributor Author

oli-obk commented Jun 9, 2015

compiling the following:

static A: i32 = 42;
static B: i32 = *&A;

will (in my modified rustc to show CheckCrateVisitor::{consume, borrow}) yield the following relevant debug messages:

###snip###
DEBUG:rustc::middle::expr_use_visitor: consume_expr(expr=expr(6: 42))
DEBUG:rustc::middle::mem_categorization: cat_expr: id=6 expr=expr(6: 42)
DEBUG:rustc::middle::mem_categorization: cat_rvalue ret {cat_rvalue(ReStatic) id:6 m:McDeclared ty:i32}
DEBUG:rustc::middle::mem_categorization: cat_rvalue_node ret {cat_rvalue(ReStatic) id:6 m:McDeclared ty:i32}
DEBUG:rustc::middle::expr_use_visitor: delegate_consume(consume_id=6, cmt={cat_rvalue(ReStatic) id:6 m:McDeclared ty:i32})
DEBUG:rustc::middle::check_const: CheckCrateVisitor::consume: cur.cat=cat_rvalue(ReStatic), mode=Static
DEBUG:rustc::middle::expr_use_visitor: walk_expr(expr=expr(6: 42))
DEBUG:rustc::middle::check_const: visit_item(item=item(static B::B (id=7)))
###snip###
DEBUG:rustc::middle::expr_use_visitor: consume_expr(expr=expr(9: *&A))
DEBUG:rustc::middle::mem_categorization: cat_expr: id=9 expr=expr(9: *&A)
DEBUG:rustc::middle::mem_categorization: cat_expr: id=10 expr=expr(10: &A)
DEBUG:rustc::middle::region: temporary_scope(10) = None
DEBUG:rustc::middle::mem_categorization: cat_rvalue ret {cat_rvalue(ReStatic) id:10 m:McDeclared ty:&i32}
DEBUG:rustc::middle::mem_categorization: cat_rvalue_node ret {cat_rvalue(ReStatic) id:10 m:McDeclared ty:&i32}
DEBUG:rustc::middle::mem_categorization: cat_deref: method_call=MethodCall { expr_id: 9, autoderef: 0 } method_ty=None
DEBUG:rustc::middle::mem_categorization: MutabilityCategory::from_borrow_kind(ImmBorrow) => McImmutable
DEBUG:rustc::middle::mem_categorization: MutabilityCategory::from_pointer_kind(McDeclared, BorrowedPtr(ImmBorrow, ReScope(Misc(9)))) => McImmutable
DEBUG:rustc::middle::mem_categorization: cat_deref_common ret {cat_rvalue(ReStatic)-&ReScope(Misc(9))0-> id:9 m:McImmutable ty:i32}
DEBUG:rustc::middle::mem_categorization: cat_deref ret {cat_rvalue(ReStatic)-&ReScope(Misc(9))0-> id:9 m:McImmutable ty:i32}
DEBUG:rustc::middle::expr_use_visitor: delegate_consume(consume_id=9, cmt={cat_rvalue(ReStatic)-&ReScope(Misc(9))0-> id:9 m:McImmutable ty:i32})
DEBUG:rustc::middle::check_const: CheckCrateVisitor::consume: cur.cat=cat_deref(cmt_ { id: 10, span: Span { lo: BytePos(515), hi: BytePos(517), expn_id: ExpnId(4294967295) }, cat: cat_rvalue(ReStatic), mutbl: McDeclared, ty: TyS { sty: ty_rptr(ReScope(Misc(9)), mt { ty: TyS { sty: ty_int(i32), flags: Cell { value: 262144 }, region_depth: 0 }, mutbl: MutImmutable }), flags: Cell { value: 32 }, region_depth: 0 }, note: NoteNone }, 0, BorrowedPtr(ImmBorrow, ReScope(Misc(9)))), mode=Static
DEBUG:rustc::middle::check_const: CheckCrateVisitor::consume: cur.cat=cat_rvalue(ReStatic), mode=Static
DEBUG:rustc::middle::expr_use_visitor: walk_expr(expr=expr(9: *&A))
DEBUG:rustc::middle::expr_use_visitor: walk_expr(expr=expr(10: &A))
DEBUG:rustc::middle::expr_use_visitor: borrow_expr(expr=expr(11: A), r=ReScope(Misc(9)), bk=ImmBorrow)
DEBUG:rustc::middle::mem_categorization: cat_expr: id=11 expr=expr(11: A)
DEBUG:rustc::middle::mem_categorization: cat_def: id=11 expr=i32 def=DefStatic(DefId { krate: 0, node: 4 }, false)
DEBUG:rustc::middle::check_const: CheckCrateVisitor::borrow: cur.cat=cat_static_item, is_interior=false, mode=Static
DEBUG:rustc::middle::expr_use_visitor: walk_expr(expr=expr(11: A))
DEBUG:rustc::middle::check_const: visit_item(item=item(fn main::main (id=12)))
###snip###

@steveklabnik steveklabnik added the A-typesystem Area: The type system label Jun 15, 2015
@oli-obk
Copy link
Contributor Author

oli-obk commented Aug 16, 2016

produces cannot refer to other statics by value, use the address-of operator or a constant instead on all of the above examples on stable rust

@oli-obk oli-obk closed this as completed Aug 16, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-typesystem Area: The type system
Projects
None yet
Development

No branches or pull requests

2 participants