Skip to content

WIP: move const prop lints before borrowck #115756

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

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ build/
/src/tools/x/target
# Created by default with `src/ci/docker/run.sh`
/obj/
# Created on ICE during build
rustc-ice-*.txt

## Temporary files
*~
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1398,7 +1398,7 @@ rustc_queries! {

/// Computes the layout of a type. Note that this implicitly
/// executes in "reveal all" mode, and will normalize the input type.
query layout_of(
query layout_of_raw(
key: ty::ParamEnvAnd<'tcx, Ty<'tcx>>
) -> Result<ty::layout::TyAndLayout<'tcx>, &'tcx ty::layout::LayoutError<'tcx>> {
depth_limit
Expand Down
33 changes: 33 additions & 0 deletions compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,39 @@ pub trait LayoutOf<'tcx>: LayoutOfHelpers<'tcx> {
}
}

impl<'tcx> TyCtxt<'tcx> {
pub fn layout_of(
self,
query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>,
) -> Result<TyAndLayout<'tcx>, &'tcx LayoutError<'tcx>> {
self.at(DUMMY_SP).layout_of(query)
}
}

impl<'tcx> TyCtxtAt<'tcx> {
pub fn layout_of(
self,
query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>,
) -> Result<TyAndLayout<'tcx>, &'tcx LayoutError<'tcx>> {
let (param_env, ty) = query.into_parts();
let param_env = param_env.with_reveal_all_normalized(self.tcx);

// FIXME: We might want to have two different versions of `layout_of`:
// One that can be called after typecheck has completed and can use
// `normalize_erasing_regions` here and another one that can be called
// before typecheck has completed and uses `try_normalize_erasing_regions`.
let ty = match self.try_normalize_erasing_regions(param_env, ty) {
Ok(t) => t,
Err(normalization_error) => {
return Err(self
.arena
.alloc(LayoutError::NormalizationFailure(ty, normalization_error)));
}
};
self.layout_of_raw(param_env.and(ty))
}
}

impl<'tcx, C: LayoutOfHelpers<'tcx>> LayoutOf<'tcx> for C {}

impl<'tcx> LayoutOfHelpers<'tcx> for LayoutCx<'tcx, TyCtxt<'tcx>> {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_query_system/src/query/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl QueryJobId {
while let Some(id) = current_id {
let info = query_map.get(&id).unwrap();
// FIXME: This string comparison should probably not be done.
if format!("{:?}", info.query.dep_kind) == "layout_of" {
if format!("{:?}", info.query.dep_kind) == "layout_of_raw" {
depth += 1;
last_layout = Some((info.clone(), depth));
}
Expand Down
25 changes: 2 additions & 23 deletions compiler/rustc_ty_utils/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,17 @@ use crate::errors::{
use crate::layout_sanity_check::sanity_check_layout;

pub fn provide(providers: &mut Providers) {
*providers = Providers { layout_of, ..*providers };
*providers = Providers { layout_of_raw, ..*providers };
}

#[instrument(skip(tcx, query), level = "debug")]
fn layout_of<'tcx>(
fn layout_of_raw<'tcx>(
tcx: TyCtxt<'tcx>,
query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>,
) -> Result<TyAndLayout<'tcx>, &'tcx LayoutError<'tcx>> {
let (param_env, ty) = query.into_parts();
debug!(?ty);

let param_env = param_env.with_reveal_all_normalized(tcx);
let unnormalized_ty = ty;

// FIXME: We might want to have two different versions of `layout_of`:
// One that can be called after typecheck has completed and can use
// `normalize_erasing_regions` here and another one that can be called
// before typecheck has completed and uses `try_normalize_erasing_regions`.
let ty = match tcx.try_normalize_erasing_regions(param_env, ty) {
Ok(t) => t,
Err(normalization_error) => {
return Err(tcx
.arena
.alloc(LayoutError::NormalizationFailure(ty, normalization_error)));
}
};

if ty != unnormalized_ty {
// Ensure this layout is also cached for the normalized type.
return tcx.layout_of(param_env.and(ty));
}

let cx = LayoutCx { tcx, param_env };

let layout = layout_of_uncached(&cx, ty)?;
Expand Down
2 changes: 1 addition & 1 deletion src/etc/rust_analyzer_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"compiler/rustc_codegen_gcc/Cargo.toml"
],
"rust-analyzer.rustfmt.overrideCommand": [
"./build/host/rustfmt/bin/rustfmt",
"${workspaceFolder}/build/host/rustfmt/bin/rustfmt",
"--edition=2021"
],
"rust-analyzer.procMacro.server": "./build/host/stage0/libexec/rust-analyzer-proc-macro-srv",
Expand Down
1 change: 0 additions & 1 deletion tests/ui/consts/const-size_of-cycle.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ note: ...which requires const-evaluating + checking `Foo::bytes::{constant#0}`..
LL | bytes: [u8; std::mem::size_of::<Foo>()]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...which requires computing layout of `Foo`...
= note: ...which requires computing layout of `[u8; std::mem::size_of::<Foo>()]`...
= note: ...which requires normalizing `[u8; std::mem::size_of::<Foo>()]`...
= note: ...which again requires evaluating type-level constant, completing the cycle
note: cycle used when checking that `Foo` is well-formed
Expand Down
1 change: 0 additions & 1 deletion tests/ui/consts/issue-44415.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ note: ...which requires const-evaluating + checking `Foo::bytes::{constant#0}`..
LL | bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...which requires computing layout of `Foo`...
= note: ...which requires computing layout of `[u8; unsafe { intrinsics::size_of::<Foo>() }]`...
= note: ...which requires normalizing `[u8; unsafe { intrinsics::size_of::<Foo>() }]`...
= note: ...which again requires evaluating type-level constant, completing the cycle
note: cycle used when checking that `Foo` is well-formed
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/layout/layout-cycle.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
error[E0391]: cycle detected when computing layout of `S<S<()>>`
|
= note: ...which requires computing layout of `<S<()> as Tr>::I`...
= note: ...which again requires computing layout of `S<S<()>>`, completing the cycle
= note: ...which immediately requires computing layout of `S<S<()>>` again
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information

error: failed to get layout for S<S<()>>: a cycle occurred during layout computation
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/layout/valid_range_oob.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
error: the compiler unexpectedly panicked. this is a bug.

query stack during panic:
#0 [layout_of] computing layout of `Foo`
#0 [layout_of_raw] computing layout of `Foo`
#1 [eval_to_allocation_raw] const-evaluating + checking `FOO`
end of query stack
3 changes: 1 addition & 2 deletions tests/ui/recursion/issue-26548-recursion-via-normalize.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//~ ERROR cycle detected when computing layout of `core::option::Option<S>`
//~| NOTE see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
//~| NOTE ...which requires computing layout of `S`...
//~| NOTE ...which requires computing layout of `core::option::Option<<S as Mirror>::It>`...
//~| NOTE ...which again requires computing layout of `core::option::Option<S>`, completing the cycle
//~| NOTE cycle used when computing layout of `core::option::Option<<S as Mirror>::It>`

trait Mirror {
//~^ NOTE: cycle used when checking deathness of variables in top-level module
type It: ?Sized;
}
impl<T: ?Sized> Mirror for T {
Expand Down
13 changes: 11 additions & 2 deletions tests/ui/recursion/issue-26548-recursion-via-normalize.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
error[E0391]: cycle detected when computing layout of `core::option::Option<S>`
|
= note: ...which requires computing layout of `S`...
= note: ...which requires computing layout of `core::option::Option<<S as Mirror>::It>`...
= note: ...which again requires computing layout of `core::option::Option<S>`, completing the cycle
= note: cycle used when computing layout of `core::option::Option<<S as Mirror>::It>`
note: cycle used when checking deathness of variables in top-level module
--> $DIR/issue-26548-recursion-via-normalize.rs:6:1
|
LL | / trait Mirror {
LL | |
LL | | type It: ?Sized;
LL | | }
... |
LL | | let _s = S(None);
LL | | }
| |_^
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information

error: aborting due to previous error
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/sized/recursive-type-2.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
error[E0391]: cycle detected when computing layout of `Foo<()>`
|
= note: ...which requires computing layout of `<() as A>::Assoc`...
= note: ...which again requires computing layout of `Foo<()>`, completing the cycle
= note: ...which immediately requires computing layout of `Foo<()>` again
note: cycle used when elaborating drops for `main`
--> $DIR/recursive-type-2.rs:11:1
|
Expand Down
1 change: 0 additions & 1 deletion tests/ui/type-alias-impl-trait/issue-53092-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ note: ...which requires type-checking `CONST_BUG`...
|
LL | const CONST_BUG: Bug<u8, ()> = unsafe { std::mem::transmute(|_: u8| ()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...which requires computing layout of `Bug<u8, ()>`...
= note: ...which requires normalizing `Bug<u8, ()>`...
= note: ...which again requires computing type of `Bug::{opaque#0}`, completing the cycle
note: cycle used when checking item types in top-level module
Expand Down