Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Do not materialise X in [X; 0] when X is unsizing a const #145277

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

Open
dingxiangfei2009 wants to merge 3 commits into rust-lang:master
base: master
Choose a base branch
Loading
from dingxiangfei2009:fold-coercion-into-const
Open
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

Some comments aren't visible on the classic Files Changed page.

24 changes: 23 additions & 1 deletion compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rustc_middle::middle::region;
use rustc_middle::mir::interpret::Scalar;
use rustc_middle::mir::*;
use rustc_middle::thir::*;
use rustc_middle::ty::adjustment::PointerCoercion;
use rustc_middle::ty::cast::{CastTy, mir_cast_kind};
use rustc_middle::ty::util::IntTypeExt;
use rustc_middle::ty::{self, Ty, UpvarArgs};
Expand Down Expand Up @@ -656,6 +657,27 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
block.and(rvalue)
}

/// Recursively inspect a THIR expression and probe through unsizing
/// operations that can be const-folded today.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this have to do with const-folding (which usually refers to the constant propagation optimization pass)?

fn check_constness(&self, mut kind: &'a ExprKind<'tcx>) -> bool {
loop {
debug!(?kind, "check_constness");
match kind {
&ExprKind::ValueTypeAscription { source: eid, user_ty: _, user_ty_span: _ }
| &ExprKind::Use { source: eid }
| &ExprKind::PointerCoercion {
cast: PointerCoercion::Unsize,
source: eid,
is_from_as_cast: _,
}
| &ExprKind::Scope { region_scope: _, lint_level: _, value: eid } => {
kind = &self.thir[eid].kind
}
_ => return matches!(Category::of(&kind), Some(Category::Constant)),
}
}
}

fn build_zero_repeat(
&mut self,
mut block: BasicBlock,
Expand All @@ -666,7 +688,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let this = self;
let value_expr = &this.thir[value];
let elem_ty = value_expr.ty;
if let Some(Category::Constant) = Category::of(&value_expr.kind) {
if this.check_constness(&value_expr.kind) {
// Repeating a const does nothing
} else {
// For a non-const, we may need to generate an appropriate `Drop`
Expand Down
46 changes: 46 additions & 0 deletions tests/ui/coercion/no_local_for_coerced_const-issue-143671.rs
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//@ run-pass

#![feature(unsize)]
#![feature(coerce_unsized)]

use std::fmt::Display;
use std::marker::Unsize;
use std::ops::CoerceUnsized;
use std::rc::Weak;

#[repr(transparent)]
struct X<'a, T: ?Sized> {
f: &'a T,
}

impl<'a, T: ?Sized> Drop for X<'a, T> {
fn drop(&mut self) {
panic!()
}
}

impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<X<'a, U>> for X<'a, T> where
&'a T: CoerceUnsized<&'a U>
{
}

const Y: X<'static, i32> = X { f: &0 };

fn main() {
let _: [X<'static, dyn Display>; 0] = [Y; 0];
coercion_on_weak_in_const();
coercion_on_weak_as_cast();
}

fn coercion_on_weak_in_const() {
const X: Weak<i32> = Weak::new();
const Y: [Weak<dyn Send>; 0] = [X; 0];
let _ = Y;
}

fn coercion_on_weak_as_cast() {
const Y: X<'static, i32> = X { f: &0 };
// What happens in the following code is that
// a constant is explicitly coerced into
let _a: [X<'static, dyn Display>; 0] = [Y as X<'static, dyn Display>; 0];
}
Loading

AltStyle によって変換されたページ (->オリジナル) /