Skip to content

Commit 41611ba

Browse files
committed
Auto merge of #30661 - michaelwoerister:trans_fn_attrs, r=nrc
So far `librustc::trans::base::trans_fn()` and `trans_closure()` have been passed the list of attributes on the function being translated *only* if the function was local and non-generic. For generic functions, functions inlined from other crates, functions with foreign ABI and for closures, only an empty list of attributes was ever passed to `trans_fn()`. This led to the case that generic functions marked with `#[rustc_mir]` where not actually translated via MIR but via the legacy translation path. This PR makes function/closure attributes always be passed to `trans_fn()` and disables the one test where this makes a difference. If there is an actual reason why attributes were not passed along in these cases, let me know. cc @rust-lang/compiler cc @luqmana regarding the test case
2 parents badc23b + ff93fc8 commit 41611ba

File tree

8 files changed

+54
-19
lines changed

8 files changed

+54
-19
lines changed

src/librustc_trans/trans/closure.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use session::config::FullDebugInfo;
3131

3232
use syntax::abi::RustCall;
3333
use syntax::ast;
34+
use syntax::attr::{ThinAttributes, ThinAttributesExt};
3435

3536
use rustc_front::hir;
3637

@@ -176,7 +177,8 @@ pub fn trans_closure_expr<'a, 'tcx>(dest: Dest<'a, 'tcx>,
176177
body: &hir::Block,
177178
id: ast::NodeId,
178179
closure_def_id: DefId, // (*)
179-
closure_substs: &'tcx ty::ClosureSubsts<'tcx>)
180+
closure_substs: &'tcx ty::ClosureSubsts<'tcx>,
181+
closure_expr_attrs: &ThinAttributes)
180182
-> Option<Block<'a, 'tcx>>
181183
{
182184
// (*) Note that in the case of inlined functions, the `closure_def_id` will be the
@@ -218,7 +220,7 @@ pub fn trans_closure_expr<'a, 'tcx>(dest: Dest<'a, 'tcx>,
218220
llfn,
219221
param_substs,
220222
id,
221-
&[],
223+
closure_expr_attrs.as_attr_slice(),
222224
sig.output,
223225
function_type.abi,
224226
ClosureEnv::Closure(closure_def_id, &freevars));

src/librustc_trans/trans/consts.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -986,8 +986,13 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
986986
hir::ExprClosure(_, ref decl, ref body) => {
987987
match ety.sty {
988988
ty::TyClosure(def_id, ref substs) => {
989-
closure::trans_closure_expr(closure::Dest::Ignore(cx), decl,
990-
body, e.id, def_id, substs);
989+
closure::trans_closure_expr(closure::Dest::Ignore(cx),
990+
decl,
991+
body,
992+
e.id,
993+
def_id,
994+
substs,
995+
&e.attrs);
991996
}
992997
_ =>
993998
cx.sess().span_bug(

src/librustc_trans/trans/expr.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,13 @@ fn trans_rvalue_dps_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
11961196
&format!("closure expr without closure type: {:?}", t)),
11971197
};
11981198

1199-
closure::trans_closure_expr(dest, decl, body, expr.id, def_id, substs).unwrap_or(bcx)
1199+
closure::trans_closure_expr(dest,
1200+
decl,
1201+
body,
1202+
expr.id,
1203+
def_id,
1204+
substs,
1205+
&expr.attrs).unwrap_or(bcx)
12001206
}
12011207
hir::ExprCall(ref f, ref args) => {
12021208
if bcx.tcx().is_method_call(expr.id) {

src/librustc_trans/trans/foreign.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ pub fn trans_rust_fn_with_foreign_abi<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
640640

641641
let llfn = declare::define_internal_rust_fn(ccx, &ps, t);
642642
attributes::from_fn_attrs(ccx, attrs, llfn);
643-
base::trans_fn(ccx, decl, body, llfn, param_substs, id, &[]);
643+
base::trans_fn(ccx, decl, body, llfn, param_substs, id, attrs);
644644
llfn
645645
}
646646

src/librustc_trans/trans/inline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ fn instantiate_inline(ccx: &CrateContext, fn_id: DefId)
165165
llfn,
166166
empty_substs,
167167
impl_item.id,
168-
&[]);
168+
&impl_item.attrs);
169169
// See linkage comments on items.
170170
if ccx.sess().opts.cg.codegen_units == 1 {
171171
SetLinkage(llfn, InternalLinkage);

src/librustc_trans/trans/meth.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,13 @@ pub fn trans_impl(ccx: &CrateContext,
7474
for (ref ccx, is_origin) in ccx.maybe_iter(trans_everywhere) {
7575
let llfn = get_item_val(ccx, impl_item.id);
7676
let empty_substs = tcx.mk_substs(Substs::trans_empty());
77-
trans_fn(ccx, &sig.decl, body, llfn,
78-
empty_substs, impl_item.id, &[]);
77+
trans_fn(ccx,
78+
&sig.decl,
79+
body,
80+
llfn,
81+
empty_substs,
82+
impl_item.id,
83+
&impl_item.attrs);
7984
update_linkage(ccx,
8085
llfn,
8186
Some(impl_item.id),

src/librustc_trans/trans/monomorphize.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,13 @@ pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
185185
ccx, &**decl, &**body, &[], d, psubsts, fn_node_id,
186186
Some(&hash[..]));
187187
} else {
188-
trans_fn(ccx, &**decl, &**body, d, psubsts, fn_node_id, &[]);
188+
trans_fn(ccx,
189+
&**decl,
190+
&**body,
191+
d,
192+
psubsts,
193+
fn_node_id,
194+
&i.attrs);
189195
}
190196
}
191197

@@ -216,7 +222,7 @@ pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
216222
d,
217223
psubsts,
218224
impl_item.id,
219-
&[]);
225+
&impl_item.attrs);
220226
}
221227
d
222228
}
@@ -232,8 +238,13 @@ pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
232238
let d = mk_lldecl(abi::Rust);
233239
let needs_body = setup_lldecl(d, &trait_item.attrs);
234240
if needs_body {
235-
trans_fn(ccx, &sig.decl, body, d,
236-
psubsts, trait_item.id, &[]);
241+
trans_fn(ccx,
242+
&sig.decl,
243+
body,
244+
d,
245+
psubsts,
246+
trait_item.id,
247+
&trait_item.attrs);
237248
}
238249
d
239250
}

src/test/run-pass/mir_trans_calls.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,16 @@ fn test5(x: &Bar, a: isize) -> isize {
5858
x.extension_method(a)
5959
}
6060

61-
#[rustc_mir]
62-
fn test6<T: Bar>(x: &T, a: isize) -> isize {
63-
// Test calling extension method on generic callee
64-
x.extension_method(a)
65-
}
61+
// FIXME #30661: Although this function has the #[rustc_mir] attribute it never
62+
// was translated via the MIR implementation because attributes
63+
// where not passed along to trans::base::trans_fn() for generic
64+
// functions.
65+
// Uncomment this test once the thing it tests is fixed.
66+
// #[rustc_mir]
67+
// fn test6<T: Bar>(x: &T, a: isize) -> isize {
68+
// // Test calling extension method on generic callee
69+
// x.extension_method(a)
70+
// }
6671

6772
trait One<T = Self> {
6873
fn one() -> T;
@@ -94,7 +99,8 @@ fn main() {
9499
assert_eq!(test3(&Foo, 42), 42);
95100
assert_eq!(test4(&Foo, 970), 970);
96101
assert_eq!(test5(&Foo, 8576), 8576);
97-
assert_eq!(test6(&Foo, 12367), 12367);
102+
// see definition of test6() above
103+
// assert_eq!(test6(&Foo, 12367), 12367);
98104
assert_eq!(test7(), 1);
99105
assert_eq!(test8(), 2);
100106
}

0 commit comments

Comments
 (0)