Skip to content

Rollup of 9 pull requests #101716

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 20 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a297631
use <[u8]>::escape_ascii instead of core::ascii::escape_default
kadiwa4 Aug 19, 2022
052887e
Add inline-llvm option for disabling/enabling LLVM inlining
Aug 8, 2022
9c3cb2d
Update browser-ui-test version to 0.10.0
GuillaumeGomez Sep 10, 2022
975dd6c
fix #101626, suggest pub instead of public for const type item
chenyukang Sep 11, 2022
fd21df7
Fix naming format of IEEE 754 standard
LingMan Sep 10, 2022
fd2766e
Check that the types in RPITITs are WF
compiler-errors Sep 11, 2022
89b6488
Deny RPITIT for object safety
compiler-errors Sep 11, 2022
c3fcfa8
Rustdoc-Json: Add tests for trait impls.
aDotInTheVoid Sep 11, 2022
d010f95
Update tests for new browser-ui-test version
GuillaumeGomez Sep 10, 2022
bff8078
rustdoc: remove no-op `#search { margin-left: 0 }`
notriddle Sep 12, 2022
2d239fb
rustdoc: remove no-op `#search`
notriddle Sep 12, 2022
4137032
Rollup merge of #100293 - yanchen4791:add-inline-llvm-option, r=nneth…
Dylan-DPC Sep 12, 2022
9317775
Rollup merge of #100767 - kadiwa4:escape_ascii, r=jackh726
Dylan-DPC Sep 12, 2022
d7bad03
Rollup merge of #101668 - chenyukang:fix-101626, r=TaKO8Ki
Dylan-DPC Sep 12, 2022
10af4fb
Rollup merge of #101671 - LingMan:ieee_754, r=Dylan-DPC
Dylan-DPC Sep 12, 2022
5faf033
Rollup merge of #101676 - compiler-errors:rpitit-wf, r=lcnr
Dylan-DPC Sep 12, 2022
6bacb6f
Rollup merge of #101681 - compiler-errors:rpitit-obj-safety, r=lcnr
Dylan-DPC Sep 12, 2022
8ef557d
Rollup merge of #101693 - GuillaumeGomez:update-browser-ui-test-0-10,…
Dylan-DPC Sep 12, 2022
228a8bd
Rollup merge of #101701 - aDotInTheVoid:rdj-impl-tests, r=GuillaumeGomez
Dylan-DPC Sep 12, 2022
9bf89e7
Rollup merge of #101706 - notriddle:notriddle/search-mobile, r=Guilla…
Dylan-DPC Sep 12, 2022
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
Prev Previous commit
Next Next commit
Deny RPITIT for object safety
  • Loading branch information
compiler-errors committed Sep 11, 2022
commit 89b6488ef0417df6f82671e57c4761a816af3974
9 changes: 9 additions & 0 deletions compiler/rustc_middle/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,12 @@ impl ObjectSafetyViolation {
ObjectSafetyViolation::Method(name, MethodViolationCode::ReferencesSelfOutput, _) => {
format!("method `{}` references the `Self` type in its return type", name).into()
}
ObjectSafetyViolation::Method(
name,
MethodViolationCode::ReferencesImplTraitInTrait,
_,
) => format!("method `{}` references an `impl Trait` type in its return type", name)
.into(),
ObjectSafetyViolation::Method(
name,
MethodViolationCode::WhereClauseReferencesSelf,
Expand Down Expand Up @@ -1014,6 +1020,9 @@ pub enum MethodViolationCode {
/// e.g., `fn foo(&self) -> Self`
ReferencesSelfOutput,

/// e.g., `fn foo(&self) -> impl Sized`
ReferencesImplTraitInTrait,

/// e.g., `fn foo(&self) where Self: Clone`
WhereClauseReferencesSelf,

Expand Down
26 changes: 26 additions & 0 deletions compiler/rustc_trait_selection/src/traits/object_safety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::elaborate_predicates;
use crate::infer::TyCtxtInferExt;
use crate::traits::query::evaluate_obligation::InferCtxtExt;
use crate::traits::{self, Obligation, ObligationCause};
use hir::def::DefKind;
use rustc_errors::{FatalError, MultiSpan};
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
Expand Down Expand Up @@ -431,6 +432,9 @@ fn virtual_call_violation_for_method<'tcx>(
if contains_illegal_self_type_reference(tcx, trait_def_id, sig.output()) {
return Some(MethodViolationCode::ReferencesSelfOutput);
}
if contains_illegal_impl_trait_in_trait(tcx, sig.output()) {
return Some(MethodViolationCode::ReferencesImplTraitInTrait);
}

// We can't monomorphize things like `fn foo<A>(...)`.
let own_counts = tcx.generics_of(method.def_id).own_counts();
Expand Down Expand Up @@ -793,6 +797,12 @@ fn contains_illegal_self_type_reference<'tcx, T: TypeVisitable<'tcx>>(
ControlFlow::CONTINUE
}
}
ty::Projection(ref data)
if self.tcx.def_kind(data.item_def_id) == DefKind::ImplTraitPlaceholder =>
{
// We'll deny these later in their own pass
ControlFlow::CONTINUE
}
ty::Projection(ref data) => {
// This is a projected type `<Foo as SomeTrait>::X`.

Expand Down Expand Up @@ -861,6 +871,22 @@ fn contains_illegal_self_type_reference<'tcx, T: TypeVisitable<'tcx>>(
.is_break()
}

pub fn contains_illegal_impl_trait_in_trait<'tcx>(
tcx: TyCtxt<'tcx>,
ty: ty::Binder<'tcx, Ty<'tcx>>,
) -> bool {
// FIXME(RPITIT): Perhaps we should use a visitor here?
ty.skip_binder().walk().any(|arg| {
if let ty::GenericArgKind::Type(ty) = arg.unpack()
&& let ty::Projection(proj) = ty.kind()
{
tcx.def_kind(proj.item_def_id) == DefKind::ImplTraitPlaceholder
} else {
false
}
})
}

pub fn provide(providers: &mut ty::query::Providers) {
*providers = ty::query::Providers { object_safety_violations, ..*providers };
}
22 changes: 22 additions & 0 deletions src/test/ui/impl-trait/in-trait/object-safety.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![feature(return_position_impl_trait_in_trait)]
#![allow(incomplete_features)]

use std::fmt::Debug;

trait Foo {
fn baz(&self) -> impl Debug;
}

impl Foo for u32 {
fn baz(&self) -> u32 {
32
}
}

fn main() {
let i = Box::new(42_u32) as Box<dyn Foo>;
//~^ ERROR the trait `Foo` cannot be made into an object
//~| ERROR the trait `Foo` cannot be made into an object
let s = i.baz();
//~^ ERROR the trait `Foo` cannot be made into an object
}
50 changes: 50 additions & 0 deletions src/test/ui/impl-trait/in-trait/object-safety.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
error[E0038]: the trait `Foo` cannot be made into an object
--> $DIR/object-safety.rs:17:33
|
LL | let i = Box::new(42_u32) as Box<dyn Foo>;
| ^^^^^^^^^^^^ `Foo` cannot be made into an object
|
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $DIR/object-safety.rs:7:8
|
LL | trait Foo {
| --- this trait cannot be made into an object...
LL | fn baz(&self) -> impl Debug;
| ^^^ ...because method `baz` references an `impl Trait` type in its return type
= help: consider moving `baz` to another trait

error[E0038]: the trait `Foo` cannot be made into an object
--> $DIR/object-safety.rs:20:13
|
LL | let s = i.baz();
| ^^^^^^^ `Foo` cannot be made into an object
|
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $DIR/object-safety.rs:7:8
|
LL | trait Foo {
| --- this trait cannot be made into an object...
LL | fn baz(&self) -> impl Debug;
| ^^^ ...because method `baz` references an `impl Trait` type in its return type
= help: consider moving `baz` to another trait

error[E0038]: the trait `Foo` cannot be made into an object
--> $DIR/object-safety.rs:17:13
|
LL | let i = Box::new(42_u32) as Box<dyn Foo>;
| ^^^^^^^^^^^^^^^^ `Foo` cannot be made into an object
|
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $DIR/object-safety.rs:7:8
|
LL | trait Foo {
| --- this trait cannot be made into an object...
LL | fn baz(&self) -> impl Debug;
| ^^^ ...because method `baz` references an `impl Trait` type in its return type
= help: consider moving `baz` to another trait
= note: required for `Box<u32>` to implement `CoerceUnsized<Box<dyn Foo>>`
= note: required by cast to type `Box<dyn Foo>`

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0038`.