Skip to content

Commit fb58db4

Browse files
committed
Require type is sized in wfcheck.check_item_type for externed DSTs, closes #36122
1 parent 142c98d commit fb58db4

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

src/librustc_typeck/check/wfcheck.rs

+34-14
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ pub fn check_item_well_formed<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: Def
118118
check_item_fn(tcx, item);
119119
}
120120
hir::ItemStatic(..) => {
121-
check_item_type(tcx, item);
121+
check_item_type(tcx, item.id, None);
122122
}
123123
hir::ItemConst(..) => {
124-
check_item_type(tcx, item);
124+
check_item_type(tcx, item.id, None);
125125
}
126126
hir::ItemStruct(ref struct_def, ref ast_generics) => {
127127
check_type_defn(tcx, item, false, |fcx| {
@@ -147,6 +147,17 @@ pub fn check_item_well_formed<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: Def
147147
hir::ItemTrait(..) => {
148148
check_trait(tcx, item);
149149
}
150+
hir::ItemForeignMod(ref foreign_mod) => {
151+
for foreign_item in foreign_mod.items.iter() {
152+
match foreign_item.node {
153+
hir::ForeignItemStatic(..) => {
154+
check_item_type(tcx, foreign_item.id,
155+
Some(ObligationCauseCode::SizedReturnType));
156+
},
157+
_ => {}
158+
}
159+
}
160+
}
150161
_ => {}
151162
}
152163
}
@@ -215,9 +226,9 @@ fn check_associated_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
215226
})
216227
}
217228

218-
fn for_item<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>, item: &hir::Item)
229+
fn for_item<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>, id: ast::NodeId)
219230
-> CheckWfFcxBuilder<'a, 'gcx, 'tcx> {
220-
for_id(tcx, item.id, item.span)
231+
for_id(tcx, id, tcx.hir.span(id))
221232
}
222233

223234
fn for_id<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>, id: ast::NodeId, span: Span)
@@ -236,7 +247,7 @@ fn check_type_defn<'a, 'tcx, F>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
236247
item: &hir::Item, all_sized: bool, mut lookup_fields: F)
237248
where F: for<'fcx, 'gcx, 'tcx2> FnMut(&FnCtxt<'fcx, 'gcx, 'tcx2>) -> Vec<AdtVariant<'tcx2>>
238249
{
239-
for_item(tcx, item).with_fcx(|fcx, fcx_tcx| {
250+
for_item(tcx, item.id).with_fcx(|fcx, fcx_tcx| {
240251
let variants = lookup_fields(fcx);
241252
let def_id = fcx.tcx.hir.local_def_id(item.id);
242253
let packed = fcx.tcx.adt_def(def_id).repr.packed();
@@ -290,14 +301,14 @@ fn check_type_defn<'a, 'tcx, F>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
290301

291302
fn check_trait<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &hir::Item) {
292303
let trait_def_id = tcx.hir.local_def_id(item.id);
293-
for_item(tcx, item).with_fcx(|fcx, _| {
304+
for_item(tcx, item.id).with_fcx(|fcx, _| {
294305
check_where_clauses(tcx, fcx, item.span, trait_def_id);
295306
vec![]
296307
});
297308
}
298309

299310
fn check_item_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &hir::Item) {
300-
for_item(tcx, item).with_fcx(|fcx, tcx| {
311+
for_item(tcx, item.id).with_fcx(|fcx, tcx| {
301312
let def_id = fcx.tcx.hir.local_def_id(item.id);
302313
let sig = fcx.tcx.fn_sig(def_id);
303314
let sig = fcx.normalize_associated_types_in(item.span, &sig);
@@ -309,15 +320,24 @@ fn check_item_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &hir::Item) {
309320
}
310321

311322
fn check_item_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
312-
item: &hir::Item)
323+
id: ast::NodeId,
324+
size_check: Option<ObligationCauseCode<'tcx>>)
313325
{
314-
debug!("check_item_type: {:?}", item);
326+
debug!("check_item_type: {:?}", tcx.hir.get(id));
315327

316-
for_item(tcx, item).with_fcx(|fcx, _this| {
317-
let ty = fcx.tcx.type_of(fcx.tcx.hir.local_def_id(item.id));
318-
let item_ty = fcx.normalize_associated_types_in(item.span, &ty);
328+
for_item(tcx, id).with_fcx(|fcx, _this| {
329+
let span = tcx.hir.span(id);
330+
let ty = fcx.tcx.type_of(fcx.tcx.hir.local_def_id(id));
331+
let item_ty = fcx.normalize_associated_types_in(span, &ty);
319332

320-
fcx.register_wf_obligation(item_ty, item.span, ObligationCauseCode::MiscObligation);
333+
fcx.register_wf_obligation(item_ty, span, ObligationCauseCode::MiscObligation);
334+
335+
match size_check {
336+
None => {}
337+
Some(code) => {
338+
fcx.require_type_is_sized(item_ty, span, code);
339+
}
340+
}
321341

322342
vec![] // no implied bounds in a const etc
323343
});
@@ -330,7 +350,7 @@ fn check_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
330350
{
331351
debug!("check_impl: {:?}", item);
332352

333-
for_item(tcx, item).with_fcx(|fcx, tcx| {
353+
for_item(tcx, item.id).with_fcx(|fcx, tcx| {
334354
let item_def_id = fcx.tcx.hir.local_def_id(item.id);
335355

336356
match *ast_trait_ref {

src/test/compile-fail/issue-36122.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2018 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+
// ignore-tidy-linelength
12+
13+
fn main() {
14+
extern "C" {
15+
static symbol: [usize];
16+
//~^ ERROR the size for value values of type `[usize]` cannot be known at compilation time [E0277]
17+
}
18+
19+
unsafe {
20+
println!("{}", symbol[0]);
21+
}
22+
}

0 commit comments

Comments
 (0)