Skip to content
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

Remove constness from ImplSource::Param #114781

Merged
merged 1 commit into from
Aug 14, 2023
Merged
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
50 changes: 23 additions & 27 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2495,35 +2495,31 @@ impl<'hir> GenericArgsCtor<'hir> {

let id = lcx.next_node_id();
let hir_id = lcx.next_id();

let Some(host_param_id) = lcx.host_param_id else {
lcx.tcx
.sess
.delay_span_bug(span, "no host param id for call in const yet no errors reported");
return;
};

let body = lcx.lower_body(|lcx| {
(
&[],
match constness {
ast::Const::Yes(_) => {
let hir_id = lcx.next_id();
let res =
Res::Def(DefKind::ConstParam, lcx.host_param_id.unwrap().to_def_id());
let expr_kind = hir::ExprKind::Path(hir::QPath::Resolved(
None,
lcx.arena.alloc(hir::Path {
span,
res,
segments: arena_vec![lcx; hir::PathSegment::new(Ident {
name: sym::host,
span,
}, hir_id, res)],
}),
));
lcx.expr(span, expr_kind)
}
ast::Const::No => lcx.expr(
(&[], {
let hir_id = lcx.next_id();
let res = Res::Def(DefKind::ConstParam, host_param_id.to_def_id());
let expr_kind = hir::ExprKind::Path(hir::QPath::Resolved(
None,
lcx.arena.alloc(hir::Path {
span,
hir::ExprKind::Lit(
lcx.arena.alloc(hir::Lit { span, node: ast::LitKind::Bool(true) }),
),
),
},
)
res,
segments: arena_vec![lcx; hir::PathSegment::new(Ident {
name: sym::host,
span,
}, hir_id, res)],
}),
));
lcx.expr(span, expr_kind)
})
});

let attr_id = lcx.tcx.sess.parse_sess.attr_id_generator.mk_attr_id();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
};

match implsrc {
Ok(Some(ImplSource::Param(ty::BoundConstness::ConstIfConst, _))) => {
Ok(Some(ImplSource::Param(_))) if tcx.features().effects => {
debug!(
"const_trait_impl: provided {:?} via where-clause in {:?}",
trait_ref, param_env
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ impl Qualif for NeedsNonConstDrop {

if !matches!(
impl_src,
ImplSource::Builtin(BuiltinImplSource::Misc, _)
| ImplSource::Param(ty::BoundConstness::ConstIfConst, _)
ImplSource::Builtin(BuiltinImplSource::Misc, _) | ImplSource::Param(_)
) {
// If our const destruct candidate is not ConstDestruct or implied by the param env,
// then it's bad
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_middle/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ pub enum ImplSource<'tcx, N> {
/// for some type parameter. The `Vec<N>` represents the
/// obligations incurred from normalizing the where-clause (if
/// any).
Param(ty::BoundConstness, Vec<N>),
Param(Vec<N>),

/// Successful resolution for a builtin impl.
Builtin(BuiltinImplSource, Vec<N>),
Expand All @@ -659,21 +659,21 @@ impl<'tcx, N> ImplSource<'tcx, N> {
pub fn nested_obligations(self) -> Vec<N> {
match self {
ImplSource::UserDefined(i) => i.nested,
ImplSource::Param(_, n) | ImplSource::Builtin(_, n) => n,
ImplSource::Param(n) | ImplSource::Builtin(_, n) => n,
}
}

pub fn borrow_nested_obligations(&self) -> &[N] {
match self {
ImplSource::UserDefined(i) => &i.nested,
ImplSource::Param(_, n) | ImplSource::Builtin(_, n) => &n,
ImplSource::Param(n) | ImplSource::Builtin(_, n) => &n,
}
}

pub fn borrow_nested_obligations_mut(&mut self) -> &mut [N] {
match self {
ImplSource::UserDefined(i) => &mut i.nested,
ImplSource::Param(_, n) | ImplSource::Builtin(_, n) => n,
ImplSource::Param(n) | ImplSource::Builtin(_, n) => n,
}
}

Expand All @@ -687,7 +687,7 @@ impl<'tcx, N> ImplSource<'tcx, N> {
args: i.args,
nested: i.nested.into_iter().map(f).collect(),
}),
ImplSource::Param(ct, n) => ImplSource::Param(ct, n.into_iter().map(f).collect()),
ImplSource::Param(n) => ImplSource::Param(n.into_iter().map(f).collect()),
ImplSource::Builtin(source, n) => {
ImplSource::Builtin(source, n.into_iter().map(f).collect())
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/traits/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pub enum SelectionCandidate<'tcx> {
/// an applicable bound in the trait definition. The `usize` is an index
/// into the list returned by `tcx.item_bounds`. The constness is the
/// constness of the bound in the trait.
// FIXME(effects) do we need this constness
ProjectionCandidate(usize, ty::BoundConstness),

/// Implementation of a `Fn`-family trait by one of the anonymous types
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/traits/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSource<'tcx, N> {
write!(f, "Builtin({source:?}, {d:?})")
}

super::ImplSource::Param(ct, n) => {
write!(f, "ImplSourceParamData({n:?}, {ct:?})")
super::ImplSource::Param(n) => {
write!(f, "ImplSourceParamData({n:?})")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl<'tcx> InferCtxtSelectExt<'tcx> for InferCtxt<'tcx> {
// It's fine not to do anything to rematch these, since there are no
// nested obligations.
(Certainty::Yes, CandidateSource::ParamEnv(_) | CandidateSource::AliasBound) => {
Ok(Some(ImplSource::Param(ty::BoundConstness::NotConst, nested_obligations)))
Ok(Some(ImplSource::Param(nested_obligations)))
}

(Certainty::Maybe(_), _) => Ok(None),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
ParamCandidate(param) => {
let obligations =
self.confirm_param_candidate(obligation, param.map_bound(|t| t.trait_ref));
// FIXME(effects)
ImplSource::Param(ty::BoundConstness::NotConst, obligations)
ImplSource::Param(obligations)
}

ImplCandidate(impl_def_id) => {
Expand All @@ -72,9 +71,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
ImplSource::Builtin(BuiltinImplSource::Misc, data)
}

ProjectionCandidate(idx, constness) => {
ProjectionCandidate(idx, _) => {
let obligations = self.confirm_projection_candidate(obligation, idx)?;
ImplSource::Param(constness, obligations)
ImplSource::Param(obligations)
}

ObjectCandidate(idx) => self.confirm_object_candidate(obligation, idx)?,
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ fn is_ty_const_destruct<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, body: &Body<'tcx>

if !matches!(
impl_src,
ImplSource::Builtin(BuiltinImplSource::Misc, _) | ImplSource::Param(ty::BoundConstness::ConstIfConst, _)
ImplSource::Builtin(BuiltinImplSource::Misc, _) | ImplSource::Param(_)
) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// known-bug: #110395
// FIXME check-pass
#![feature(const_trait_impl)]
#![feature(const_trait_impl, effects)]

#[const_trait]
trait Foo {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
error[E0015]: cannot call non-const fn `<<T as Foo>::Assoc as Foo>::foo` in constant functions
error[E0277]: the trait bound `T: Foo` is not satisfied
--> $DIR/assoc-type-const-bound-usage.rs:12:5
|
LL | <T as Foo>::Assoc::foo();
| ^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `T`
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
help: consider further restricting this bound
|
LL | const fn foo<T: ~const Foo + Foo>() {
| +++++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0015`.
For more information about this error, try `rustc --explain E0277`.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// known-bug: #110395
#![feature(const_trait_impl)]
// FIXME(effects)
// check-pass
#![feature(const_trait_impl, effects)]

pub const fn equals_self<T: PartialEq>(t: &T) -> bool {
*t == *t
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Tests that a const default trait impl can be specialized by another const
// trait impl and that the specializing impl will be used during const-eval.

// known-bug: #110395
// FIXME run-pass
// run-pass

#![feature(const_trait_impl)]
#![feature(const_trait_impl, effects)]
#![feature(min_specialization)]

#[const_trait]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// known-bug: #110395
// FIXME run-pass

#![feature(const_trait_impl)]
#![feature(const_trait_impl, effects)]
#![feature(min_specialization)]

#[const_trait]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
error[E0015]: cannot call non-const fn `<T as Value>::value` in constant functions
--> $DIR/non-const-default-const-specialized.rs:15:5
error[E0119]: conflicting implementations of trait `Value` for type `FortyTwo`
--> $DIR/non-const-default-const-specialized.rs:27:1
|
LL | T::value()
| ^^^^^^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
LL | impl<T> Value for T {
| ------------------- first implementation here
...
LL | impl const Value for FortyTwo {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `FortyTwo`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0015`.
For more information about this error, try `rustc --explain E0119`.
5 changes: 2 additions & 3 deletions tests/ui/rfcs/rfc-2632-const-trait-impl/super-traits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// known-bug: #110395
// FIXME check-pass
#![feature(const_trait_impl)]
// check-pass
#![feature(const_trait_impl, effects)]

#[const_trait]
trait Foo {
Expand Down
11 changes: 0 additions & 11 deletions tests/ui/rfcs/rfc-2632-const-trait-impl/super-traits.stderr

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#![feature(const_trait_impl)]
#![feature(const_trait_impl, effects)]
#![feature(generic_arg_infer)]
#![feature(generic_const_exprs)]
#![allow(incomplete_features)]

struct Foo<const N: usize>;

impl<const N: usize> Foo<N> {
fn add<A: ~const Add42>(self) -> Foo<{ A::add(N) }> {
Foo
}
fn add<A: ~const Add42>(self) -> Foo<{ A::add(N) }> {
//~^ ERROR mismatched types
Foo
}
}

#[const_trait]
Expand All @@ -24,7 +25,7 @@ impl const Add42 for () {

fn bar<A: ~const Add42, const N: usize>(_: Foo<N>) -> Foo<{ A::add(N) }> {
//~^ ERROR `~const` is not allowed here
//~| ERROR cannot call
//~| ERROR mismatched types
Foo
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
error: `~const` is not allowed here
--> $DIR/tilde-const-and-const-params.rs:25:11
--> $DIR/tilde-const-and-const-params.rs:26:11
|
LL | fn bar<A: ~const Add42, const N: usize>(_: Foo<N>) -> Foo<{ A::add(N) }> {
| ^^^^^^^^^^^^
|
note: this function is not `const`, so it cannot have `~const` trait bounds
--> $DIR/tilde-const-and-const-params.rs:25:4
--> $DIR/tilde-const-and-const-params.rs:26:4
|
LL | fn bar<A: ~const Add42, const N: usize>(_: Foo<N>) -> Foo<{ A::add(N) }> {
| ^^^

error[E0015]: cannot call non-const fn `<A as Add42>::add` in constants
--> $DIR/tilde-const-and-const-params.rs:25:61
error[E0308]: mismatched types
--> $DIR/tilde-const-and-const-params.rs:26:61
|
LL | fn bar<A: ~const Add42, const N: usize>(_: Foo<N>) -> Foo<{ A::add(N) }> {
| ^^^^^^^^^
| ^^^^^^^^^ expected `false`, found `true`
|
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
= note: expected constant `false`
found constant `true`

error: aborting due to 2 previous errors
error[E0308]: mismatched types
--> $DIR/tilde-const-and-const-params.rs:9:44
|
LL | fn add<A: ~const Add42>(self) -> Foo<{ A::add(N) }> {
| ^^^^^^^^^ expected `false`, found `true`
|
= note: expected constant `false`
found constant `true`

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0015`.
For more information about this error, try `rustc --explain E0308`.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// known-bug: #110395
// FIXME check-pass
#![feature(const_trait_impl)]
#![feature(const_trait_impl, effects)]

#[const_trait]
trait Foo {
Expand Down
Loading