Skip to content

Commit 065a5b0

Browse files
author
Michael Kainer
committed
Fixes ICE when using reexported unit-like structs
Fixes that unit-like structs cannot be used if they are reexported and used in another crate. The compiler fails with an ICE, because unit-like structs are exported as DefFn and the expression `UnitStruct` is interpreted as function pointer instead of a call to the constructor. To resolve this ambiguity tuple-like struct constructors are now exported as CtorFn. When `rustc::metadata::decoder` finds a CtorFn it sets a new flag `is_ctor` in DefFn to true. Relevant changes are in `rustc::metadata::{encoder, decoder}` and in `rustc::middle::ty`. Closes #12660 and #16973.
1 parent d2f8d4c commit 065a5b0

File tree

19 files changed

+81
-32
lines changed

19 files changed

+81
-32
lines changed

src/librustc/metadata/decoder.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ enum Family {
112112
MutStatic, // b
113113
Fn, // f
114114
UnsafeFn, // u
115+
CtorFn, // o
115116
StaticMethod, // F
116117
UnsafeStaticMethod, // U
117118
Type, // y
@@ -135,6 +136,7 @@ fn item_family(item: rbml::Doc) -> Family {
135136
'b' => MutStatic,
136137
'f' => Fn,
137138
'u' => UnsafeFn,
139+
'o' => CtorFn,
138140
'F' => StaticMethod,
139141
'U' => UnsafeStaticMethod,
140142
'y' => Type,
@@ -304,8 +306,9 @@ fn item_to_def_like(item: rbml::Doc, did: ast::DefId, cnum: ast::CrateNum)
304306
ImmStatic => DlDef(def::DefStatic(did, false)),
305307
MutStatic => DlDef(def::DefStatic(did, true)),
306308
Struct => DlDef(def::DefStruct(did)),
307-
UnsafeFn => DlDef(def::DefFn(did, ast::UnsafeFn)),
308-
Fn => DlDef(def::DefFn(did, ast::NormalFn)),
309+
UnsafeFn => DlDef(def::DefFn(did, ast::UnsafeFn, false)),
310+
Fn => DlDef(def::DefFn(did, ast::NormalFn, false)),
311+
CtorFn => DlDef(def::DefFn(did, ast::NormalFn, true)),
309312
StaticMethod | UnsafeStaticMethod => {
310313
let fn_style = if fam == UnsafeStaticMethod {
311314
ast::UnsafeFn

src/librustc/metadata/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ fn encode_info_for_struct_ctor(ecx: &EncodeContext,
760760

761761
rbml_w.start_tag(tag_items_data_item);
762762
encode_def_id(rbml_w, local_def(ctor_id));
763-
encode_family(rbml_w, 'f');
763+
encode_family(rbml_w, 'o');
764764
encode_bounds_and_type(rbml_w, ecx,
765765
&lookup_item_type(ecx.tcx, local_def(ctor_id)));
766766
encode_name(rbml_w, name.name);

src/librustc/middle/astencode.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ fn decode_def(dcx: &DecodeContext, doc: rbml::Doc) -> def::Def {
440440
impl tr for def::Def {
441441
fn tr(&self, dcx: &DecodeContext) -> def::Def {
442442
match *self {
443-
def::DefFn(did, p) => def::DefFn(did.tr(dcx), p),
443+
def::DefFn(did, p, is_ctor) => def::DefFn(did.tr(dcx), p, is_ctor),
444444
def::DefStaticMethod(did, wrapped_did2, p) => {
445445
def::DefStaticMethod(did.tr(dcx),
446446
match wrapped_did2 {

src/librustc/middle/check_const.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ fn check_expr(v: &mut CheckCrateVisitor, e: &Expr) {
133133
}
134134
match v.tcx.def_map.borrow().find(&e.id) {
135135
Some(&DefStatic(..)) |
136-
Some(&DefFn(_, _)) |
136+
Some(&DefFn(..)) |
137137
Some(&DefVariant(_, _, _)) |
138138
Some(&DefStruct(_)) => { }
139139

src/librustc/middle/def.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use syntax::ast_util::local_def;
1414

1515
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
1616
pub enum Def {
17-
DefFn(ast::DefId, ast::FnStyle),
17+
DefFn(ast::DefId, ast::FnStyle, bool /* is_ctor */),
1818
DefStaticMethod(/* method */ ast::DefId, MethodProvenance, ast::FnStyle),
1919
DefSelfTy(/* trait id */ ast::NodeId),
2020
DefMod(ast::DefId),
@@ -57,7 +57,7 @@ pub enum MethodProvenance {
5757
impl Def {
5858
pub fn def_id(&self) -> ast::DefId {
5959
match *self {
60-
DefFn(id, _) | DefStaticMethod(id, _, _) | DefMod(id) |
60+
DefFn(id, _, _) | DefStaticMethod(id, _, _) | DefMod(id) |
6161
DefForeignMod(id) | DefStatic(id, _) |
6262
DefVariant(_, id, _) | DefTy(id, _) | DefAssociatedTy(id) |
6363
DefTyParam(_, id, _) | DefUse(id) | DefStruct(id) | DefTrait(id) |

src/librustc/middle/intrinsicck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for IntrinsicCheckingVisitor<'a, 'tcx> {
121121
match expr.node {
122122
ast::ExprPath(..) => {
123123
match ty::resolve_expr(self.tcx, expr) {
124-
DefFn(did, _) if self.def_id_is_transmute(did) => {
124+
DefFn(did, _, _) if self.def_id_is_transmute(did) => {
125125
let typ = ty::node_id_to_type(self.tcx, expr.id);
126126
match ty::get(typ).sty {
127127
ty_bare_fn(ref bare_fn_ty)

src/librustc/middle/privacy.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
934934
}
935935
// Tuple struct constructors across crates are identified as
936936
// DefFn types, so we explicitly handle that case here.
937-
Some(&def::DefFn(did, _)) if !is_local(did) => {
937+
Some(&def::DefFn(did, _, _)) if !is_local(did) => {
938938
match csearch::get_tuple_struct_definition_if_ctor(
939939
&self.tcx.sess.cstore, did) {
940940
Some(did) => guard(did),

src/librustc/middle/resolve.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,7 @@ impl<'a> Resolver<'a> {
12471247
let name_bindings =
12481248
self.add_child(ident, parent.clone(), ForbidDuplicateValues, sp);
12491249

1250-
let def = DefFn(local_def(item.id), fn_style);
1250+
let def = DefFn(local_def(item.id), fn_style, false);
12511251
name_bindings.define_value(def, sp, is_public);
12521252
parent
12531253
}
@@ -1705,7 +1705,7 @@ impl<'a> Resolver<'a> {
17051705

17061706
match foreign_item.node {
17071707
ForeignItemFn(_, ref generics) => {
1708-
let def = DefFn(local_def(foreign_item.id), UnsafeFn);
1708+
let def = DefFn(local_def(foreign_item.id), UnsafeFn, false);
17091709
name_bindings.define_value(def, foreign_item.span, is_public);
17101710

17111711
self.with_type_parameter_rib(
@@ -2022,7 +2022,8 @@ impl<'a> Resolver<'a> {
20222022
DUMMY_SP);
20232023
let def = DefFn(
20242024
static_method_info.def_id,
2025-
static_method_info.fn_style);
2025+
static_method_info.fn_style,
2026+
false);
20262027

20272028
method_name_bindings.define_value(
20282029
def, DUMMY_SP,
@@ -2591,7 +2592,8 @@ impl<'a> Resolver<'a> {
25912592

25922593
match value_result {
25932594
BoundResult(ref target_module, ref name_bindings) => {
2594-
debug!("(resolving single import) found value target");
2595+
debug!("(resolving single import) found value target: {:?}",
2596+
{ name_bindings.value_def.borrow().clone().unwrap().def });
25952597
self.check_for_conflicting_import(
25962598
&import_resolution.value_target,
25972599
directive.span,

src/librustc/middle/save/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
234234
def::DefVariant(_, _, _) |
235235
def::DefUpvar(..) => Some(recorder::VarRef),
236236

237-
def::DefFn(_, _) => Some(recorder::FnRef),
237+
def::DefFn(..) => Some(recorder::FnRef),
238238

239239
def::DefSelfTy(_) |
240240
def::DefRegion(_) |
@@ -792,10 +792,10 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
792792
Some(declid),
793793
self.cur_scope);
794794
},
795-
def::DefFn(def_id, _) => self.fmt.fn_call_str(ex.span,
796-
sub_span,
797-
def_id,
798-
self.cur_scope),
795+
def::DefFn(def_id, _, _) => self.fmt.fn_call_str(ex.span,
796+
sub_span,
797+
def_id,
798+
self.cur_scope),
799799
_ => self.sess.span_bug(ex.span,
800800
format!("Unexpected def kind while looking up path in '{}'",
801801
self.span.snippet(ex.span)).as_slice()),
@@ -808,7 +808,7 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
808808
def::DefLocal(_) |
809809
def::DefStatic(_,_) |
810810
def::DefStruct(_) |
811-
def::DefFn(_, _) => self.write_sub_paths_truncated(path),
811+
def::DefFn(..) => self.write_sub_paths_truncated(path),
812812
_ => {},
813813
}
814814

src/librustc/middle/trans/callee.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ fn trans<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, expr: &ast::Expr)
142142
debug!("trans_def(def={}, ref_expr={})", def.repr(bcx.tcx()), ref_expr.repr(bcx.tcx()));
143143
let expr_ty = node_id_type(bcx, ref_expr.id);
144144
match def {
145-
def::DefFn(did, _) if {
145+
def::DefFn(did, _, _) if {
146146
let maybe_def_id = inline::get_local_instance(bcx.ccx(), did);
147147
let maybe_ast_node = maybe_def_id.and_then(|def_id| bcx.tcx().map
148148
.find(def_id.node));
@@ -157,15 +157,15 @@ fn trans<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, expr: &ast::Expr)
157157
data: NamedTupleConstructor(substs, 0)
158158
}
159159
}
160-
def::DefFn(did, _) if match ty::get(expr_ty).sty {
160+
def::DefFn(did, _, _) if match ty::get(expr_ty).sty {
161161
ty::ty_bare_fn(ref f) => f.abi == synabi::RustIntrinsic,
162162
_ => false
163163
} => {
164164
let substs = node_id_substs(bcx, ExprId(ref_expr.id));
165165
let def_id = inline::maybe_instantiate_inline(bcx.ccx(), did);
166166
Callee { bcx: bcx, data: Intrinsic(def_id.node, substs) }
167167
}
168-
def::DefFn(did, _) |
168+
def::DefFn(did, _, _) |
169169
def::DefStaticMethod(did, def::FromImpl(_), _) => {
170170
fn_callee(bcx, trans_fn_ref(bcx, did, ExprId(ref_expr.id)))
171171
}

src/librustc/middle/trans/closure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ pub fn get_wrapper_for_bare_fn(ccx: &CrateContext,
532532
is_local: bool) -> ValueRef {
533533

534534
let def_id = match def {
535-
def::DefFn(did, _) | def::DefStaticMethod(did, _, _) |
535+
def::DefFn(did, _, _) | def::DefStaticMethod(did, _, _) |
536536
def::DefVariant(_, did, _) | def::DefStruct(did) => did,
537537
_ => {
538538
ccx.sess().bug(format!("get_wrapper_for_bare_fn: \

src/librustc/middle/trans/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ fn const_expr_unadjusted(cx: &CrateContext, e: &ast::Expr,
623623

624624
let opt_def = cx.tcx().def_map.borrow().find_copy(&e.id);
625625
match opt_def {
626-
Some(def::DefFn(def_id, _fn_style)) => {
626+
Some(def::DefFn(def_id, _fn_style, _)) => {
627627
if !ast_util::is_local(def_id) {
628628
let ty = csearch::get_type(cx.tcx(), def_id).ty;
629629
(base::trans_external_path(cx, def_id, ty), true)

src/librustc/middle/trans/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,7 @@ fn trans_def_fn_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
11941194
let _icx = push_ctxt("trans_def_datum_unadjusted");
11951195

11961196
let llfn = match def {
1197-
def::DefFn(did, _) |
1197+
def::DefFn(did, _, _) |
11981198
def::DefStruct(did) | def::DefVariant(_, did, _) |
11991199
def::DefStaticMethod(did, def::FromImpl(_), _) => {
12001200
callee::trans_fn_ref(bcx, did, ExprId(ref_expr.id))

src/librustc/middle/ty.rs

+6
Original file line numberDiff line numberDiff line change
@@ -3588,6 +3588,12 @@ pub fn expr_kind(tcx: &ctxt, expr: &ast::Expr) -> ExprKind {
35883588
}
35893589
}
35903590

3591+
// Special case: A unit like struct's constructor must be called without () at the
3592+
// end (like `UnitStruct`) which means this is an ExprPath to a DefFn. But in case
3593+
// of unit structs this is should not be interpretet as function pointer but as
3594+
// call to the constructor.
3595+
def::DefFn(_, _, true) => RvalueDpsExpr,
3596+
35913597
// Fn pointers are just scalar values.
35923598
def::DefFn(..) | def::DefStaticMethod(..) => RvalueDatumExpr,
35933599

src/librustc/middle/typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5082,7 +5082,7 @@ pub fn polytype_for_def(fcx: &FnCtxt,
50825082
let typ = fcx.local_ty(sp, nid);
50835083
return no_params(typ);
50845084
}
5085-
def::DefFn(id, _) | def::DefStaticMethod(id, _, _) |
5085+
def::DefFn(id, _, _) | def::DefStaticMethod(id, _, _) |
50865086
def::DefStatic(id, _) | def::DefVariant(_, id, _) |
50875087
def::DefStruct(id) => {
50885088
return ty::lookup_item_type(fcx.ccx.tcx, id);

src/librustdoc/clean/inline.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,8 @@ fn try_inline_def(cx: &DocContext, tcx: &ty::ctxt,
7373
record_extern_fqn(cx, did, clean::TypeTrait);
7474
clean::TraitItem(build_external_trait(cx, tcx, did))
7575
}
76-
def::DefFn(did, style) => {
76+
def::DefFn(did, style, false) => {
7777
// If this function is a tuple struct constructor, we just skip it
78-
if csearch::get_tuple_struct_definition_if_ctor(&tcx.sess.cstore,
79-
did).is_some() {
80-
return None
81-
}
8278
record_extern_fqn(cx, did, clean::TypeFunction);
8379
clean::FunctionItem(build_external_function(cx, tcx, did, style))
8480
}

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2078,7 +2078,7 @@ fn resolve_type(cx: &DocContext, path: Path,
20782078

20792079
fn register_def(cx: &DocContext, def: def::Def) -> ast::DefId {
20802080
let (did, kind) = match def {
2081-
def::DefFn(i, _) => (i, TypeFunction),
2081+
def::DefFn(i, _, _) => (i, TypeFunction),
20822082
def::DefTy(i, false) => (i, TypeTypedef),
20832083
def::DefTy(i, true) => (i, TypeEnum),
20842084
def::DefTrait(i) => (i, TypeTrait),

src/test/auxiliary/issue-12660-aux.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type="lib"]
12+
#![crate_name="issue12660aux"]
13+
14+
pub use my_mod::{MyStruct, my_fn};
15+
16+
mod my_mod {
17+
pub struct MyStruct;
18+
19+
pub fn my_fn(my_struct: MyStruct) {
20+
}
21+
}

src/test/run-pass/issue-12660.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:issue-12660-aux.rs
12+
13+
extern crate issue12660aux;
14+
15+
use issue12660aux::{my_fn, MyStruct};
16+
17+
#[allow(path_statement)]
18+
fn main() {
19+
my_fn(MyStruct);
20+
MyStruct;
21+
}

0 commit comments

Comments
 (0)