Skip to content

udpdate error message for unsized union field #43901

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

Merged
merged 2 commits into from
Aug 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use syntax::ptr::P;
use syntax::symbol::{Symbol, keywords};
use syntax::tokenstream::TokenStream;
use syntax::util::ThinVec;
use ty::AdtKind;

use rustc_data_structures::indexed_vec;

Expand Down Expand Up @@ -1789,6 +1790,15 @@ impl Item_ {
ItemDefaultImpl(..) => "item",
}
}

pub fn adt_kind(&self) -> Option<AdtKind> {
match *self {
ItemStruct(..) => Some(AdtKind::Struct),
ItemUnion(..) => Some(AdtKind::Union),
ItemEnum(..) => Some(AdtKind::Enum),
_ => None,
}
}
}

/// A reference from an trait to one of its associated items. This
Expand Down
18 changes: 14 additions & 4 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,8 +1111,19 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
ObligationCauseCode::StructInitializerSized => {
err.note("structs must have a statically known size to be initialized");
}
ObligationCauseCode::FieldSized => {
err.note("only the last field of a struct may have a dynamically sized type");
ObligationCauseCode::FieldSized(ref item) => {
match *item {
AdtKind::Struct => {
err.note("only the last field of a struct may have a dynamically \
sized type");
}
AdtKind::Union => {
err.note("no field of a union may have a dynamically sized type");
}
AdtKind::Enum => {
err.note("no field of an enum variant may have a dynamically sized type");
}
}
}
ObligationCauseCode::ConstSized => {
err.note("constant expressions must have a statically known size");
Expand Down Expand Up @@ -1154,8 +1165,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
fn suggest_new_overflow_limit(&self, err: &mut DiagnosticBuilder) {
let current_limit = self.tcx.sess.recursion_limit.get();
let suggested_limit = current_limit * 2;
err.help(&format!(
"consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate",
err.help(&format!("consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate",
suggested_limit));
}
}
4 changes: 2 additions & 2 deletions src/librustc/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use hir::def_id::DefId;
use middle::region::RegionMaps;
use middle::free_region::FreeRegionMap;
use ty::subst::Substs;
use ty::{self, Ty, TyCtxt, TypeFoldable, ToPredicate};
use ty::{self, AdtKind, Ty, TyCtxt, TypeFoldable, ToPredicate};
use ty::error::{ExpectedFound, TypeError};
use infer::{InferCtxt};

Expand Down Expand Up @@ -133,7 +133,7 @@ pub enum ObligationCauseCode<'tcx> {
RepeatVec,

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

/// Constant expressions must be sized.
ConstSized,
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/traits/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl<'a, 'tcx> Lift<'tcx> for traits::ObligationCauseCode<'a> {
super::ReturnType(id) => Some(super::ReturnType(id)),
super::SizedReturnType => Some(super::SizedReturnType),
super::RepeatVec => Some(super::RepeatVec),
super::FieldSized => Some(super::FieldSized),
super::FieldSized(item) => Some(super::FieldSized(item)),
super::ConstSized => Some(super::ConstSized),
super::SharedStatic => Some(super::SharedStatic),
super::BuiltinDerivedObligation(ref cause) => {
Expand Down Expand Up @@ -484,7 +484,7 @@ impl<'tcx> TypeFoldable<'tcx> for traits::ObligationCauseCode<'tcx> {
super::SizedReturnType |
super::ReturnNoExpression |
super::RepeatVec |
super::FieldSized |
super::FieldSized(_) |
super::ConstSized |
super::SharedStatic |
super::BlockTailExpression(_) |
Expand Down Expand Up @@ -532,7 +532,7 @@ impl<'tcx> TypeFoldable<'tcx> for traits::ObligationCauseCode<'tcx> {
super::SizedReturnType |
super::ReturnNoExpression |
super::RepeatVec |
super::FieldSized |
super::FieldSized(_) |
super::ConstSized |
super::SharedStatic |
super::BlockTailExpression(_) |
Expand Down
5 changes: 4 additions & 1 deletion src/librustc_typeck/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,10 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
fcx.tcx.require_lang_item(lang_items::SizedTraitLangItem),
traits::ObligationCause::new(field.span,
fcx.body_id,
traits::FieldSized));
traits::FieldSized(match item.node.adt_kind() {
Some(i) => i,
None => bug!(),
})));
}

// All field types must be well-formed.
Expand Down
26 changes: 26 additions & 0 deletions src/test/ui/union-sized-field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(untagged_unions)]

union Foo<T: ?Sized> {
value: T,
}

struct Foo2<T: ?Sized> {
value: T,
t: u32,
}

enum Foo3<T: ?Sized> {
Value(T),
}

fn main() {}
32 changes: 32 additions & 0 deletions src/test/ui/union-sized-field.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
error[E0277]: the trait bound `T: std::marker::Sized` is not satisfied
--> $DIR/union-sized-field.rs:14:5
|
14 | value: T,
| ^^^^^^^^ `T` does not have a constant size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `T`
= help: consider adding a `where T: std::marker::Sized` bound
= note: no field of a union may have a dynamically sized type

error[E0277]: the trait bound `T: std::marker::Sized` is not satisfied
--> $DIR/union-sized-field.rs:18:5
|
18 | value: T,
| ^^^^^^^^ `T` does not have a constant size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `T`
= help: consider adding a `where T: std::marker::Sized` bound
= note: only the last field of a struct may have a dynamically sized type

error[E0277]: the trait bound `T: std::marker::Sized` is not satisfied
--> $DIR/union-sized-field.rs:23:11
|
23 | Value(T),
| ^^ `T` does not have a constant size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `T`
= help: consider adding a `where T: std::marker::Sized` bound
= note: no field of an enum variant may have a dynamically sized type

error: aborting due to 3 previous errors