Skip to content

Commit ff23ad3

Browse files
committedDec 2, 2021
Auto merge of #91469 - matthiaskrgr:rollup-xom3j55, r=matthiaskrgr
Rollup of 12 pull requests Successful merges: - #89954 (Fix legacy_const_generic doc arguments display) - #91321 (Handle placeholder regions in NLL type outlive constraints) - #91329 (Fix incorrect usage of `EvaluatedToOk` when evaluating `TypeOutlives`) - #91364 (Improve error message for incorrect field accesses through raw pointers) - #91387 (Clarify and tidy up explanation of E0038) - #91410 (Move `#![feature(const_precise_live_drops)]` checks earlier in the pipeline) - #91435 (Improve diagnostic for missing half of binary operator in `if` condition) - #91444 (disable tests in Miri that take too long) - #91457 (Add additional test from rust issue number 91068) - #91460 (Document how `last_os_error` should be used) - #91464 (Document file path case sensitivity) - #91466 (Improve the comments in `Symbol::interner`.) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents acbe444 + f7afd46 commit ff23ad3

39 files changed

+712
-86
lines changed
 

‎compiler/rustc_borrowck/src/type_check/constraint_conversion.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_infer::infer::region_constraints::{GenericKind, VerifyBound};
66
use rustc_infer::infer::{self, InferCtxt, SubregionOrigin};
77
use rustc_middle::mir::ConstraintCategory;
88
use rustc_middle::ty::subst::GenericArgKind;
9+
use rustc_middle::ty::TypeFoldable;
910
use rustc_middle::ty::{self, TyCtxt};
1011
use rustc_span::DUMMY_SP;
1112

@@ -95,11 +96,23 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
9596
self.add_outlives(r1_vid, r2_vid);
9697
}
9798

98-
GenericArgKind::Type(t1) => {
99+
GenericArgKind::Type(mut t1) => {
99100
// we don't actually use this for anything, but
100101
// the `TypeOutlives` code needs an origin.
101102
let origin = infer::RelateParamBound(DUMMY_SP, t1, None);
102103

104+
// Placeholder regions need to be converted now because it may
105+
// create new region variables, which can't be done later when
106+
// verifying these bounds.
107+
if t1.has_placeholders() {
108+
t1 = tcx.fold_regions(&t1, &mut false, |r, _| match *r {
109+
ty::RegionKind::RePlaceholder(placeholder) => {
110+
self.constraints.placeholder_region(self.infcx, placeholder)
111+
}
112+
_ => r,
113+
});
114+
}
115+
103116
TypeOutlives::new(
104117
&mut *self,
105118
tcx,

‎compiler/rustc_const_eval/src/transform/check_consts/post_drop_elaboration.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ impl Visitor<'tcx> for CheckLiveDrops<'mir, 'tcx> {
8080
trace!("visit_terminator: terminator={:?} location={:?}", terminator, location);
8181

8282
match &terminator.kind {
83-
mir::TerminatorKind::Drop { place: dropped_place, .. } => {
83+
mir::TerminatorKind::Drop { place: dropped_place, .. }
84+
| mir::TerminatorKind::DropAndReplace { place: dropped_place, .. } => {
8485
let dropped_ty = dropped_place.ty(self.body, self.tcx).ty;
8586
if !NeedsNonConstDrop::in_any_value_of_ty(self.ccx, dropped_ty) {
8687
// Instead of throwing a bug, we just return here. This is because we have to
@@ -104,11 +105,6 @@ impl Visitor<'tcx> for CheckLiveDrops<'mir, 'tcx> {
104105
}
105106
}
106107

107-
mir::TerminatorKind::DropAndReplace { .. } => span_bug!(
108-
terminator.source_info.span,
109-
"`DropAndReplace` should be removed by drop elaboration",
110-
),
111-
112108
mir::TerminatorKind::Abort
113109
| mir::TerminatorKind::Call { .. }
114110
| mir::TerminatorKind::Assert { .. }

‎compiler/rustc_error_codes/src/error_codes/E0038.md

+65-32
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,64 @@
1-
Trait objects like `Box<Trait>` can only be constructed when certain
2-
requirements are satisfied by the trait in question.
3-
4-
Trait objects are a form of dynamic dispatch and use a dynamically sized type
5-
for the inner type. So, for a given trait `Trait`, when `Trait` is treated as a
6-
type, as in `Box<Trait>`, the inner type is 'unsized'. In such cases the boxed
7-
pointer is a 'fat pointer' that contains an extra pointer to a table of methods
8-
(among other things) for dynamic dispatch. This design mandates some
9-
restrictions on the types of traits that are allowed to be used in trait
10-
objects, which are collectively termed as 'object safety' rules.
11-
12-
Attempting to create a trait object for a non object-safe trait will trigger
13-
this error.
14-
15-
There are various rules:
16-
17-
### The trait cannot require `Self: Sized`
18-
19-
When `Trait` is treated as a type, the type does not implement the special
20-
`Sized` trait, because the type does not have a known size at compile time and
21-
can only be accessed behind a pointer. Thus, if we have a trait like the
22-
following:
1+
For any given trait `Trait` there may be a related _type_ called the _trait
2+
object type_ which is typically written as `dyn Trait`. In earlier editions of
3+
Rust, trait object types were written as plain `Trait` (just the name of the
4+
trait, written in type positions) but this was a bit too confusing, so we now
5+
write `dyn Trait`.
6+
7+
Some traits are not allowed to be used as trait object types. The traits that
8+
are allowed to be used as trait object types are called "object-safe" traits.
9+
Attempting to use a trait object type for a trait that is not object-safe will
10+
trigger error E0038.
11+
12+
Two general aspects of trait object types give rise to the restrictions:
13+
14+
1. Trait object types are dynamically sized types (DSTs), and trait objects of
15+
these types can only be accessed through pointers, such as `&dyn Trait` or
16+
`Box<dyn Trait>`. The size of such a pointer is known, but the size of the
17+
`dyn Trait` object pointed-to by the pointer is _opaque_ to code working
18+
with it, and different tait objects with the same trait object type may
19+
have different sizes.
20+
21+
2. The pointer used to access a trait object is paired with an extra pointer
22+
to a "virtual method table" or "vtable", which is used to implement dynamic
23+
dispatch to the object's implementations of the trait's methods. There is a
24+
single such vtable for each trait implementation, but different trait
25+
objects with the same trait object type may point to vtables from different
26+
implementations.
27+
28+
The specific conditions that violate object-safety follow, most of which relate
29+
to missing size information and vtable polymorphism arising from these aspects.
30+
31+
### The trait requires `Self: Sized`
32+
33+
Traits that are declared as `Trait: Sized` or which otherwise inherit a
34+
constraint of `Self:Sized` are not object-safe.
35+
36+
The reasoning behind this is somewhat subtle. It derives from the fact that Rust
37+
requires (and defines) that every trait object type `dyn Trait` automatically
38+
implements `Trait`. Rust does this to simplify error reporting and ease
39+
interoperation between static and dynamic polymorphism. For example, this code
40+
works:
2341

2442
```
25-
trait Foo where Self: Sized {
43+
trait Trait {
44+
}
45+
46+
fn static_foo<T:Trait + ?Sized>(b: &T) {
47+
}
2648
49+
fn dynamic_bar(a: &dyn Trait) {
50+
static_foo(a)
2751
}
2852
```
2953

30-
We cannot create an object of type `Box<Foo>` or `&Foo` since in this case
31-
`Self` would not be `Sized`.
54+
This code works because `dyn Trait`, if it exists, always implements `Trait`.
55+
56+
However as we know, any `dyn Trait` is also unsized, and so it can never
57+
implement a sized trait like `Trait:Sized`. So, rather than allow an exception
58+
to the rule that `dyn Trait` always implements `Trait`, Rust chooses to prohibit
59+
such a `dyn Trait` from existing at all.
60+
61+
Only unsized traits are considered object-safe.
3262

3363
Generally, `Self: Sized` is used to indicate that the trait should not be used
3464
as a trait object. If the trait comes from your own crate, consider removing
@@ -67,7 +97,7 @@ trait Trait {
6797
fn foo(&self) -> Self;
6898
}
6999
70-
fn call_foo(x: Box<Trait>) {
100+
fn call_foo(x: Box<dyn Trait>) {
71101
let y = x.foo(); // What type is y?
72102
// ...
73103
}
@@ -76,7 +106,8 @@ fn call_foo(x: Box<Trait>) {
76106
If only some methods aren't object-safe, you can add a `where Self: Sized` bound
77107
on them to mark them as explicitly unavailable to trait objects. The
78108
functionality will still be available to all other implementers, including
79-
`Box<Trait>` which is itself sized (assuming you `impl Trait for Box<Trait>`).
109+
`Box<dyn Trait>` which is itself sized (assuming you `impl Trait for Box<dyn
110+
Trait>`).
80111

81112
```
82113
trait Trait {
@@ -115,7 +146,9 @@ impl Trait for u8 {
115146
```
116147

117148
At compile time each implementation of `Trait` will produce a table containing
118-
the various methods (and other items) related to the implementation.
149+
the various methods (and other items) related to the implementation, which will
150+
be used as the virtual method table for a `dyn Trait` object derived from that
151+
implementation.
119152

120153
This works fine, but when the method gains generic parameters, we can have a
121154
problem.
@@ -174,7 +207,7 @@ Now, if we have the following code:
174207
# impl Trait for u8 { fn foo<T>(&self, on: T) {} }
175208
# impl Trait for bool { fn foo<T>(&self, on: T) {} }
176209
# // etc.
177-
fn call_foo(thing: Box<Trait>) {
210+
fn call_foo(thing: Box<dyn Trait>) {
178211
thing.foo(true); // this could be any one of the 8 types above
179212
thing.foo(1);
180213
thing.foo("hello");
@@ -200,7 +233,7 @@ trait Trait {
200233
```
201234

202235
If this is not an option, consider replacing the type parameter with another
203-
trait object (e.g., if `T: OtherTrait`, use `on: Box<OtherTrait>`). If the
236+
trait object (e.g., if `T: OtherTrait`, use `on: Box<dyn OtherTrait>`). If the
204237
number of types you intend to feed to this method is limited, consider manually
205238
listing out the methods of different types.
206239

@@ -226,7 +259,7 @@ trait Foo {
226259
}
227260
```
228261

229-
### The trait cannot contain associated constants
262+
### Trait contains associated constants
230263

231264
Just like static functions, associated constants aren't stored on the method
232265
table. If the trait or any subtrait contain an associated constant, they cannot
@@ -248,7 +281,7 @@ trait Foo {
248281
}
249282
```
250283

251-
### The trait cannot use `Self` as a type parameter in the supertrait listing
284+
### Trait uses `Self` as a type parameter in the supertrait listing
252285

253286
This is similar to the second sub-error, but subtler. It happens in situations
254287
like the following:

‎compiler/rustc_middle/src/mir/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1803,6 +1803,16 @@ impl<V, T> ProjectionElem<V, T> {
18031803
| Self::Downcast(_, _) => false,
18041804
}
18051805
}
1806+
1807+
/// Returns `true` if this is a `Downcast` projection with the given `VariantIdx`.
1808+
pub fn is_downcast_to(&self, v: VariantIdx) -> bool {
1809+
matches!(*self, Self::Downcast(_, x) if x == v)
1810+
}
1811+
1812+
/// Returns `true` if this is a `Field` projection with the given index.
1813+
pub fn is_field_to(&self, f: Field) -> bool {
1814+
matches!(*self, Self::Field(x, _) if x == f)
1815+
}
18061816
}
18071817

18081818
/// Alias for projections as they appear in places, where the base is a place

‎compiler/rustc_mir_transform/src/lib.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ mod match_branches;
6060
mod multiple_return_terminators;
6161
mod normalize_array_len;
6262
mod nrvo;
63+
mod remove_false_edges;
6364
mod remove_noop_landing_pads;
6465
mod remove_storage_markers;
66+
mod remove_uninit_drops;
6567
mod remove_unneeded_drops;
6668
mod remove_zsts;
6769
mod required_consts;
@@ -75,7 +77,7 @@ mod simplify_try;
7577
mod uninhabited_enum_branching;
7678
mod unreachable_prop;
7779

78-
use rustc_const_eval::transform::check_consts;
80+
use rustc_const_eval::transform::check_consts::{self, ConstCx};
7981
use rustc_const_eval::transform::promote_consts;
8082
use rustc_const_eval::transform::validate;
8183
use rustc_mir_dataflow::rustc_peek;
@@ -444,8 +446,20 @@ fn mir_drops_elaborated_and_const_checked<'tcx>(
444446
let (body, _) = tcx.mir_promoted(def);
445447
let mut body = body.steal();
446448

449+
// IMPORTANT
450+
remove_false_edges::RemoveFalseEdges.run_pass(tcx, &mut body);
451+
452+
// Do a little drop elaboration before const-checking if `const_precise_live_drops` is enabled.
453+
//
454+
// FIXME: Can't use `run_passes` for these, since `run_passes` SILENTLY DOES NOTHING IF THE MIR
455+
// PHASE DOESN'T CHANGE.
456+
if check_consts::post_drop_elaboration::checking_enabled(&ConstCx::new(tcx, &body)) {
457+
simplify::SimplifyCfg::new("remove-false-edges").run_pass(tcx, &mut body);
458+
remove_uninit_drops::RemoveUninitDrops.run_pass(tcx, &mut body);
459+
check_consts::post_drop_elaboration::check_live_drops(tcx, &body);
460+
}
461+
447462
run_post_borrowck_cleanup_passes(tcx, &mut body);
448-
check_consts::post_drop_elaboration::check_live_drops(tcx, &body);
449463
tcx.alloc_steal_mir(body)
450464
}
451465

@@ -455,7 +469,7 @@ fn run_post_borrowck_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tc
455469

456470
let post_borrowck_cleanup: &[&dyn MirPass<'tcx>] = &[
457471
// Remove all things only needed by analysis
458-
&simplify_branches::SimplifyBranches::new("initial"),
472+
&simplify_branches::SimplifyConstCondition::new("initial"),
459473
&remove_noop_landing_pads::RemoveNoopLandingPads,
460474
&cleanup_post_borrowck::CleanupNonCodegenStatements,
461475
&simplify::SimplifyCfg::new("early-opt"),
@@ -514,13 +528,13 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
514528
&instcombine::InstCombine,
515529
&separate_const_switch::SeparateConstSwitch,
516530
&const_prop::ConstProp,
517-
&simplify_branches::SimplifyBranches::new("after-const-prop"),
531+
&simplify_branches::SimplifyConstCondition::new("after-const-prop"),
518532
&early_otherwise_branch::EarlyOtherwiseBranch,
519533
&simplify_comparison_integral::SimplifyComparisonIntegral,
520534
&simplify_try::SimplifyArmIdentity,
521535
&simplify_try::SimplifyBranchSame,
522536
&dest_prop::DestinationPropagation,
523-
&simplify_branches::SimplifyBranches::new("final"),
537+
&simplify_branches::SimplifyConstCondition::new("final"),
524538
&remove_noop_landing_pads::RemoveNoopLandingPads,
525539
&simplify::SimplifyCfg::new("final"),
526540
&nrvo::RenameReturnPlace,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use rustc_middle::mir::{Body, TerminatorKind};
2+
use rustc_middle::ty::TyCtxt;
3+
4+
use crate::MirPass;
5+
6+
/// Removes `FalseEdge` and `FalseUnwind` terminators from the MIR.
7+
///
8+
/// These are only needed for borrow checking, and can be removed afterwards.
9+
///
10+
/// FIXME: This should probably have its own MIR phase.
11+
pub struct RemoveFalseEdges;
12+
13+
impl<'tcx> MirPass<'tcx> for RemoveFalseEdges {
14+
fn run_pass(&self, _: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
15+
for block in body.basic_blocks_mut() {
16+
let terminator = block.terminator_mut();
17+
terminator.kind = match terminator.kind {
18+
TerminatorKind::FalseEdge { real_target, .. } => {
19+
TerminatorKind::Goto { target: real_target }
20+
}
21+
TerminatorKind::FalseUnwind { real_target, .. } => {
22+
TerminatorKind::Goto { target: real_target }
23+
}
24+
25+
_ => continue,
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)
Please sign in to comment.