Skip to content

Commit b8ce1a3

Browse files
committed
Auto merge of #43901 - GuillaumeGomez:unsized-union-field, r=petrochenkov
udpdate error message for unsized union field Fixes #36312.
2 parents e6ab511 + c3c99b9 commit b8ce1a3

File tree

7 files changed

+91
-10
lines changed

7 files changed

+91
-10
lines changed

src/librustc/hir/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use syntax::ptr::P;
4242
use syntax::symbol::{Symbol, keywords};
4343
use syntax::tokenstream::TokenStream;
4444
use syntax::util::ThinVec;
45+
use ty::AdtKind;
4546

4647
use rustc_data_structures::indexed_vec;
4748

@@ -1789,6 +1790,15 @@ impl Item_ {
17891790
ItemDefaultImpl(..) => "item",
17901791
}
17911792
}
1793+
1794+
pub fn adt_kind(&self) -> Option<AdtKind> {
1795+
match *self {
1796+
ItemStruct(..) => Some(AdtKind::Struct),
1797+
ItemUnion(..) => Some(AdtKind::Union),
1798+
ItemEnum(..) => Some(AdtKind::Enum),
1799+
_ => None,
1800+
}
1801+
}
17921802
}
17931803

17941804
/// A reference from an trait to one of its associated items. This

src/librustc/traits/error_reporting.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -1111,8 +1111,19 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
11111111
ObligationCauseCode::StructInitializerSized => {
11121112
err.note("structs must have a statically known size to be initialized");
11131113
}
1114-
ObligationCauseCode::FieldSized => {
1115-
err.note("only the last field of a struct may have a dynamically sized type");
1114+
ObligationCauseCode::FieldSized(ref item) => {
1115+
match *item {
1116+
AdtKind::Struct => {
1117+
err.note("only the last field of a struct may have a dynamically \
1118+
sized type");
1119+
}
1120+
AdtKind::Union => {
1121+
err.note("no field of a union may have a dynamically sized type");
1122+
}
1123+
AdtKind::Enum => {
1124+
err.note("no field of an enum variant may have a dynamically sized type");
1125+
}
1126+
}
11161127
}
11171128
ObligationCauseCode::ConstSized => {
11181129
err.note("constant expressions must have a statically known size");
@@ -1154,8 +1165,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
11541165
fn suggest_new_overflow_limit(&self, err: &mut DiagnosticBuilder) {
11551166
let current_limit = self.tcx.sess.recursion_limit.get();
11561167
let suggested_limit = current_limit * 2;
1157-
err.help(&format!(
1158-
"consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate",
1168+
err.help(&format!("consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate",
11591169
suggested_limit));
11601170
}
11611171
}

src/librustc/traits/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use hir::def_id::DefId;
2020
use middle::region::RegionMaps;
2121
use middle::free_region::FreeRegionMap;
2222
use ty::subst::Substs;
23-
use ty::{self, Ty, TyCtxt, TypeFoldable, ToPredicate};
23+
use ty::{self, AdtKind, Ty, TyCtxt, TypeFoldable, ToPredicate};
2424
use ty::error::{ExpectedFound, TypeError};
2525
use infer::{InferCtxt};
2626

@@ -133,7 +133,7 @@ pub enum ObligationCauseCode<'tcx> {
133133
RepeatVec,
134134

135135
/// Types of fields (other than the last) in a struct must be sized.
136-
FieldSized,
136+
FieldSized(AdtKind),
137137

138138
/// Constant expressions must be sized.
139139
ConstSized,

src/librustc/traits/structural_impls.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl<'a, 'tcx> Lift<'tcx> for traits::ObligationCauseCode<'a> {
195195
super::ReturnType(id) => Some(super::ReturnType(id)),
196196
super::SizedReturnType => Some(super::SizedReturnType),
197197
super::RepeatVec => Some(super::RepeatVec),
198-
super::FieldSized => Some(super::FieldSized),
198+
super::FieldSized(item) => Some(super::FieldSized(item)),
199199
super::ConstSized => Some(super::ConstSized),
200200
super::SharedStatic => Some(super::SharedStatic),
201201
super::BuiltinDerivedObligation(ref cause) => {
@@ -484,7 +484,7 @@ impl<'tcx> TypeFoldable<'tcx> for traits::ObligationCauseCode<'tcx> {
484484
super::SizedReturnType |
485485
super::ReturnNoExpression |
486486
super::RepeatVec |
487-
super::FieldSized |
487+
super::FieldSized(_) |
488488
super::ConstSized |
489489
super::SharedStatic |
490490
super::BlockTailExpression(_) |
@@ -532,7 +532,7 @@ impl<'tcx> TypeFoldable<'tcx> for traits::ObligationCauseCode<'tcx> {
532532
super::SizedReturnType |
533533
super::ReturnNoExpression |
534534
super::RepeatVec |
535-
super::FieldSized |
535+
super::FieldSized(_) |
536536
super::ConstSized |
537537
super::SharedStatic |
538538
super::BlockTailExpression(_) |

src/librustc_typeck/check/wfcheck.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,10 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
233233
fcx.tcx.require_lang_item(lang_items::SizedTraitLangItem),
234234
traits::ObligationCause::new(field.span,
235235
fcx.body_id,
236-
traits::FieldSized));
236+
traits::FieldSized(match item.node.adt_kind() {
237+
Some(i) => i,
238+
None => bug!(),
239+
})));
237240
}
238241

239242
// All field types must be well-formed.

src/test/ui/union-sized-field.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2017 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+
#![feature(untagged_unions)]
12+
13+
union Foo<T: ?Sized> {
14+
value: T,
15+
}
16+
17+
struct Foo2<T: ?Sized> {
18+
value: T,
19+
t: u32,
20+
}
21+
22+
enum Foo3<T: ?Sized> {
23+
Value(T),
24+
}
25+
26+
fn main() {}

src/test/ui/union-sized-field.stderr

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error[E0277]: the trait bound `T: std::marker::Sized` is not satisfied
2+
--> $DIR/union-sized-field.rs:14:5
3+
|
4+
14 | value: T,
5+
| ^^^^^^^^ `T` does not have a constant size known at compile-time
6+
|
7+
= help: the trait `std::marker::Sized` is not implemented for `T`
8+
= help: consider adding a `where T: std::marker::Sized` bound
9+
= note: no field of a union may have a dynamically sized type
10+
11+
error[E0277]: the trait bound `T: std::marker::Sized` is not satisfied
12+
--> $DIR/union-sized-field.rs:18:5
13+
|
14+
18 | value: T,
15+
| ^^^^^^^^ `T` does not have a constant size known at compile-time
16+
|
17+
= help: the trait `std::marker::Sized` is not implemented for `T`
18+
= help: consider adding a `where T: std::marker::Sized` bound
19+
= note: only the last field of a struct may have a dynamically sized type
20+
21+
error[E0277]: the trait bound `T: std::marker::Sized` is not satisfied
22+
--> $DIR/union-sized-field.rs:23:11
23+
|
24+
23 | Value(T),
25+
| ^^ `T` does not have a constant size known at compile-time
26+
|
27+
= help: the trait `std::marker::Sized` is not implemented for `T`
28+
= help: consider adding a `where T: std::marker::Sized` bound
29+
= note: no field of an enum variant may have a dynamically sized type
30+
31+
error: aborting due to 3 previous errors
32+

0 commit comments

Comments
 (0)