Skip to content

Commit cd8377d

Browse files
committed
Auto merge of #67866 - GuillaumeGomez:rollup-32vsg5b, r=GuillaumeGomez
Rollup of 4 pull requests Successful merges: - #67822 (Revert `const_err` lint checking of casts) - #67823 (improve some `Drop`-related error messages) - #67837 (Clean up err codes) - #67848 (Remove unused `#[link_name = "m"]` attributes) Failed merges: r? @ghost
2 parents 79cf5e4 + a86a189 commit cd8377d

23 files changed

+204
-218
lines changed

src/librustc_error_codes/error_codes/E0136.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
A binary can only have one entry point, and by default that entry point is the
2-
function `main()`. If there are multiple such functions, please rename one.
1+
More than one `main` function was found.
32

43
Erroneous code example:
54

@@ -14,3 +13,7 @@ fn main() { // error!
1413
// ...
1514
}
1615
```
16+
17+
A binary can only have one entry point, and by default that entry point is the
18+
`main()` function. If there are multiple instances of this function, please
19+
rename one of them.

src/librustc_error_codes/error_codes/E0161.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
A value was moved. However, its size was not known at compile time, and only
2-
values of a known size can be moved.
1+
A value was moved whose size was not known at compile time.
32

43
Erroneous code example:
54

+29-9
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
1-
This error means that an attempt was made to match a struct type enum
2-
variant as a non-struct type:
1+
Something which is neither a tuple struct nor a tuple variant was used as a
2+
pattern.
3+
4+
Erroneous code example:
35

46
```compile_fail,E0164
5-
enum Foo { B { i: u32 } }
7+
enum A {
8+
B,
9+
C,
10+
}
11+
12+
impl A {
13+
fn new() {}
14+
}
615
7-
fn bar(foo: Foo) -> u32 {
16+
fn bar(foo: A) {
817
match foo {
9-
Foo::B(i) => i, // error E0164
18+
A::new() => (), // error!
19+
_ => {}
1020
}
1121
}
1222
```
1323

14-
Try using `{}` instead:
24+
This error means that an attempt was made to match something which is neither a
25+
tuple struct nor a tuple variant. Only these two elements are allowed as a
26+
pattern:
1527

1628
```
17-
enum Foo { B { i: u32 } }
29+
enum A {
30+
B,
31+
C,
32+
}
33+
34+
impl A {
35+
fn new() {}
36+
}
1837
19-
fn bar(foo: Foo) -> u32 {
38+
fn bar(foo: A) {
2039
match foo {
21-
Foo::B{i} => i,
40+
A::B => (), // ok!
41+
_ => {}
2242
}
2343
}
2444
```

src/librustc_mir/transform/const_prop.rs

+7-63
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ use rustc::mir::visit::{
1212
MutVisitor, MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor,
1313
};
1414
use rustc::mir::{
15-
read_only, AggregateKind, BasicBlock, BinOp, Body, BodyAndCache, CastKind, ClearCrossCrate,
16-
Constant, Local, LocalDecl, LocalKind, Location, Operand, Place, PlaceBase,
17-
ReadOnlyBodyAndCache, Rvalue, SourceInfo, SourceScope, SourceScopeData, Statement,
18-
StatementKind, Terminator, TerminatorKind, UnOp, RETURN_PLACE,
15+
read_only, AggregateKind, BasicBlock, BinOp, Body, BodyAndCache, ClearCrossCrate, Constant,
16+
Local, LocalDecl, LocalKind, Location, Operand, Place, PlaceBase, ReadOnlyBodyAndCache, Rvalue,
17+
SourceInfo, SourceScope, SourceScopeData, Statement, StatementKind, Terminator, TerminatorKind,
18+
UnOp, RETURN_PLACE,
1919
};
2020
use rustc::ty::layout::{
2121
HasDataLayout, HasTyCtxt, LayoutError, LayoutOf, Size, TargetDataLayout, TyLayout,
@@ -29,9 +29,9 @@ use syntax::ast::Mutability;
2929

3030
use crate::const_eval::error_to_const_error;
3131
use crate::interpret::{
32-
self, intern_const_alloc_recursive, truncate, AllocId, Allocation, Frame, ImmTy, Immediate,
33-
InterpCx, LocalState, LocalValue, Memory, MemoryKind, OpTy, Operand as InterpOperand, PlaceTy,
34-
Pointer, ScalarMaybeUndef, StackPopCleanup,
32+
self, intern_const_alloc_recursive, AllocId, Allocation, Frame, ImmTy, Immediate, InterpCx,
33+
LocalState, LocalValue, Memory, MemoryKind, OpTy, Operand as InterpOperand, PlaceTy, Pointer,
34+
ScalarMaybeUndef, StackPopCleanup,
3535
};
3636
use crate::rustc::ty::subst::Subst;
3737
use crate::transform::{MirPass, MirSource};
@@ -539,57 +539,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
539539
Some(())
540540
}
541541

542-
fn check_cast(
543-
&mut self,
544-
op: &Operand<'tcx>,
545-
ty: Ty<'tcx>,
546-
source_info: SourceInfo,
547-
place_layout: TyLayout<'tcx>,
548-
) -> Option<()> {
549-
if !ty.is_integral() || !op.ty(&self.local_decls, self.tcx).is_integral() {
550-
return Some(());
551-
}
552-
553-
let value = self.use_ecx(source_info, |this| {
554-
this.ecx.read_immediate(this.ecx.eval_operand(op, None)?)
555-
})?;
556-
557-
// Do not try to read bits for ZSTs. This can occur when casting an enum with one variant
558-
// to an integer. Such enums are represented as ZSTs but still have a discriminant value
559-
// which can be casted.
560-
if value.layout.is_zst() {
561-
return Some(());
562-
}
563-
564-
let value_size = value.layout.size;
565-
let value_bits = value.to_scalar().and_then(|r| r.to_bits(value_size));
566-
if let Ok(value_bits) = value_bits {
567-
let truncated = truncate(value_bits, place_layout.size);
568-
if truncated != value_bits {
569-
let scope = source_info.scope;
570-
let lint_root = match &self.source_scopes[scope].local_data {
571-
ClearCrossCrate::Set(data) => data.lint_root,
572-
ClearCrossCrate::Clear => return None,
573-
};
574-
self.tcx.lint_hir(
575-
::rustc::lint::builtin::CONST_ERR,
576-
lint_root,
577-
source_info.span,
578-
&format!(
579-
"truncating cast: the value {} requires {} bits but the target type is \
580-
only {} bits",
581-
value_bits,
582-
value_size.bits(),
583-
place_layout.size.bits()
584-
),
585-
);
586-
return None;
587-
}
588-
}
589-
590-
Some(())
591-
}
592-
593542
fn const_prop(
594543
&mut self,
595544
rvalue: &Rvalue<'tcx>,
@@ -651,11 +600,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
651600
}
652601
}
653602

654-
Rvalue::Cast(CastKind::Misc, op, ty) => {
655-
trace!("checking Cast(Misc, {:?}, {:?})", op, ty);
656-
self.check_cast(op, ty, source_info, place_layout)?;
657-
}
658-
659603
_ => {}
660604
}
661605

src/librustc_typeck/check/dropck.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ pub fn check_drop_impl(tcx: TyCtxt<'_>, drop_impl_did: DefId) -> Result<(), Erro
4747

4848
ensure_drop_predicates_are_implied_by_item_defn(
4949
tcx,
50-
drop_impl_did,
5150
dtor_predicates,
5251
adt_def.did,
5352
self_to_impl_substs,
@@ -95,16 +94,23 @@ fn ensure_drop_params_and_item_params_correspond<'tcx>(
9594
}
9695
Err(_) => {
9796
let item_span = tcx.def_span(self_type_did);
97+
let self_descr = tcx
98+
.def_kind(self_type_did)
99+
.map(|kind| kind.descr(self_type_did))
100+
.unwrap_or("type");
98101
struct_span_err!(
99102
tcx.sess,
100103
drop_impl_span,
101104
E0366,
102-
"Implementations of Drop cannot be specialized"
105+
"`Drop` impls cannot be specialized"
103106
)
104107
.span_note(
105108
item_span,
106-
"Use same sequence of generic type and region \
107-
parameters that is on the struct/enum definition",
109+
&format!(
110+
"use the same sequence of generic type, lifetime and const parameters \
111+
as the {} definition",
112+
self_descr,
113+
),
108114
)
109115
.emit();
110116
return Err(ErrorReported);
@@ -143,7 +149,6 @@ fn ensure_drop_params_and_item_params_correspond<'tcx>(
143149
/// implied by assuming the predicates attached to self_type_did.
144150
fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
145151
tcx: TyCtxt<'tcx>,
146-
drop_impl_did: DefId,
147152
dtor_predicates: ty::GenericPredicates<'tcx>,
148153
self_type_did: DefId,
149154
self_to_impl_substs: SubstsRef<'tcx>,
@@ -187,8 +192,6 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
187192

188193
let self_type_hir_id = tcx.hir().as_local_hir_id(self_type_did).unwrap();
189194

190-
let drop_impl_span = tcx.def_span(drop_impl_did);
191-
192195
// We can assume the predicates attached to struct/enum definition
193196
// hold.
194197
let generic_assumptions = tcx.predicates_of(self_type_did);
@@ -205,7 +208,7 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
205208
// just to look for all the predicates directly.
206209

207210
assert_eq!(dtor_predicates.parent, None);
208-
for (predicate, _) in dtor_predicates.predicates {
211+
for (predicate, predicate_sp) in dtor_predicates.predicates {
209212
// (We do not need to worry about deep analysis of type
210213
// expressions etc because the Drop impls are already forced
211214
// to take on a structure that is roughly an alpha-renaming of
@@ -241,18 +244,17 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
241244

242245
if !assumptions_in_impl_context.iter().any(predicate_matches_closure) {
243246
let item_span = tcx.hir().span(self_type_hir_id);
247+
let self_descr =
248+
tcx.def_kind(self_type_did).map(|kind| kind.descr(self_type_did)).unwrap_or("type");
244249
struct_span_err!(
245250
tcx.sess,
246-
drop_impl_span,
251+
*predicate_sp,
247252
E0367,
248-
"The requirement `{}` is added only by the Drop impl.",
249-
predicate
250-
)
251-
.span_note(
252-
item_span,
253-
"The same requirement must be part of \
254-
the struct/enum definition",
253+
"`Drop` impl requires `{}` but the {} it is implemented for does not",
254+
predicate,
255+
self_descr,
255256
)
257+
.span_note(item_span, "the implementor must specify the same requirement")
256258
.emit();
257259
result = Err(ErrorReported);
258260
}

src/librustc_typeck/coherence/builtin.rs

+18-29
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use rustc::ty::util::CopyImplementationError;
1313
use rustc::ty::TypeFoldable;
1414
use rustc::ty::{self, Ty, TyCtxt};
1515

16-
use hir::Node;
1716
use rustc::hir::def_id::DefId;
1817
use rustc::hir::{self, ItemKind};
1918

@@ -51,35 +50,25 @@ impl<'tcx> Checker<'tcx> {
5150
}
5251

5352
fn visit_implementation_of_drop(tcx: TyCtxt<'_>, impl_did: DefId) {
54-
if let ty::Adt(..) = tcx.type_of(impl_did).kind {
55-
/* do nothing */
56-
} else {
57-
// Destructors only work on nominal types.
58-
if let Some(impl_hir_id) = tcx.hir().as_local_hir_id(impl_did) {
59-
if let Some(Node::Item(item)) = tcx.hir().find(impl_hir_id) {
60-
let span = match item.kind {
61-
ItemKind::Impl(.., ref ty, _) => ty.span,
62-
_ => item.span,
63-
};
64-
struct_span_err!(
65-
tcx.sess,
66-
span,
67-
E0120,
68-
"the Drop trait may only be implemented on \
69-
structures"
70-
)
71-
.span_label(span, "implementing Drop requires a struct")
72-
.emit();
73-
} else {
74-
bug!("didn't find impl in ast map");
75-
}
76-
} else {
77-
bug!(
78-
"found external impl of Drop trait on \
79-
something other than a struct"
80-
);
81-
}
53+
// Destructors only work on nominal types.
54+
if let ty::Adt(..) | ty::Error = tcx.type_of(impl_did).kind {
55+
return;
8256
}
57+
58+
let impl_hir_id = tcx.hir().as_local_hir_id(impl_did).expect("foreign Drop impl on non-ADT");
59+
let sp = match tcx.hir().expect_item(impl_hir_id).kind {
60+
ItemKind::Impl(.., ty, _) => ty.span,
61+
_ => bug!("expected Drop impl item"),
62+
};
63+
64+
struct_span_err!(
65+
tcx.sess,
66+
sp,
67+
E0120,
68+
"the `Drop` trait may only be implemented for structs, enums, and unions",
69+
)
70+
.span_label(sp, "must be a struct, enum, or union")
71+
.emit();
8372
}
8473

8574
fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: DefId) {

src/libstd/sys/unix/cmath.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use libc::{c_double, c_float};
44

5-
#[link_name = "m"]
65
extern "C" {
76
pub fn acos(n: c_double) -> c_double;
87
pub fn acosf(n: c_float) -> c_float;

src/libstd/sys/vxworks/cmath.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use libc::{c_double, c_float};
44

5-
#[link_name = "m"]
65
extern "C" {
76
pub fn acos(n: c_double) -> c_double;
87
pub fn acosf(n: c_float) -> c_float;

src/libstd/sys/windows/cmath.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use libc::{c_double, c_float};
44

5-
#[link_name = "m"]
65
extern "C" {
76
pub fn acos(n: c_double) -> c_double;
87
pub fn asin(n: c_double) -> c_double;
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
// build-fail
2-
// ignore-tidy-linelength
1+
// check-pass
2+
3+
enum Foo {
4+
Bar = -42,
5+
Baz = 42,
6+
}
37

48
fn main() {
59
let _ = 0u8 as u32;
6-
let _ = (1u32 << 31) as u16; //~ ERROR truncating cast: the value 2147483648 requires 32 bits but the target type is only 16 bits
7-
let _ = (1u16 << 15) as u8; //~ ERROR truncating cast: the value 32768 requires 16 bits but the target type is only 8 bits
8-
let _ = (!0u16) as u8; //~ ERROR truncating cast: the value 65535 requires 16 bits but the target type is only 8 bits
10+
let _ = (1u32 << 31) as u16;
11+
let _ = (1u16 << 15) as u8;
12+
let _ = (!0u16) as u8;
13+
let _ = (-1i16) as i8;
14+
let _ = (Foo::Bar) as i8;
915
}

src/test/ui/consts/const-prop-overflowing-casts.stderr

-22
This file was deleted.

0 commit comments

Comments
 (0)