Skip to content

Commit

Permalink
Rollup merge of rust-lang#132227 - compiler-errors:better-const-span,…
Browse files Browse the repository at this point in the history
… r=Nadrieril

Pass constness with span into lower_poly_trait_ref

Gives us a span to point at for ~const/const on non-const traits.

Split from rust-lang#132209. r? Nadrieril
  • Loading branch information
jieyouxu authored Oct 28, 2024
2 parents afc93a9 + bd95695 commit a9ee1d0
Show file tree
Hide file tree
Showing 38 changed files with 241 additions and 254 deletions.
7 changes: 0 additions & 7 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
match hir_bound {
hir::GenericBound::Trait(poly_trait_ref) => {
let hir::TraitBoundModifiers { constness, polarity } = poly_trait_ref.modifiers;
// FIXME: We could pass these directly into `lower_poly_trait_ref`
// so that we could use these spans in diagnostics within that function...
let constness = match constness {
hir::BoundConstness::Never => None,
hir::BoundConstness::Always(_) => Some(ty::BoundConstness::Const),
hir::BoundConstness::Maybe(_) => Some(ty::BoundConstness::ConstIfConst),
};
let polarity = match polarity {
rustc_ast::BoundPolarity::Positive => ty::PredicatePolarity::Positive,
rustc_ast::BoundPolarity::Negative(_) => ty::PredicatePolarity::Negative,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
} = self.lower_poly_trait_ref(
&trait_bound.trait_ref,
trait_bound.span,
None,
hir::BoundConstness::Never,
ty::PredicatePolarity::Positive,
dummy_self,
&mut bounds,
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
&self,
trait_ref: &hir::TraitRef<'tcx>,
span: Span,
constness: Option<ty::BoundConstness>,
constness: hir::BoundConstness,
polarity: ty::PredicatePolarity,
self_ty: Ty<'tcx>,
bounds: &mut Bounds<'tcx>,
Expand All @@ -681,11 +681,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
Some(self_ty),
);

if let Some(constness) = constness
if let hir::BoundConstness::Always(span) | hir::BoundConstness::Maybe(span) = constness
&& !self.tcx().is_const_trait(trait_def_id)
{
self.dcx().emit_err(crate::errors::ConstBoundForNonConstTrait {
span: trait_ref.path.span,
span,
modifier: constness.as_str(),
});
}
Expand All @@ -708,7 +708,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
bounds.push_trait_bound(tcx, poly_trait_ref, span, polarity);

match constness {
Some(ty::BoundConstness::Const) => {
hir::BoundConstness::Always(span) => {
if polarity == ty::PredicatePolarity::Positive {
bounds.push_const_bound(
tcx,
Expand All @@ -718,13 +718,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
);
}
}
Some(ty::BoundConstness::ConstIfConst) => {
hir::BoundConstness::Maybe(_) => {
// We don't emit a const bound here, since that would mean that we
// unconditionally need to prove a `HostEffect` predicate, even when
// the predicates are being instantiated in a non-const context. This
// is instead handled in the `const_conditions` query.
}
None => {}
hir::BoundConstness::Never => {}
}
}
// On the flip side, when filtering `ConstIfConst` bounds, we only need to convert
Expand All @@ -734,12 +734,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
// here because we only call this on self bounds, and deal with the recursive case
// in `lower_assoc_item_constraint`.
PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => match constness {
Some(ty::BoundConstness::ConstIfConst) => {
hir::BoundConstness::Maybe(span) => {
if polarity == ty::PredicatePolarity::Positive {
bounds.push_const_bound(tcx, poly_trait_ref, ty::HostPolarity::Maybe, span);
}
}
None | Some(ty::BoundConstness::Const) => {}
hir::BoundConstness::Always(_) | hir::BoundConstness::Never => {}
},
}

Expand Down
8 changes: 4 additions & 4 deletions tests/ui/consts/const-block-const-bound.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/const-block-const-bound.rs:8:22
--> $DIR/const-block-const-bound.rs:8:15
|
LL | const fn f<T: ~const Destruct>(x: T) {}
| ^^^^^^^^
| ^^^^^^

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/const-block-const-bound.rs:8:22
--> $DIR/const-block-const-bound.rs:8:15
|
LL | const fn f<T: ~const Destruct>(x: T) {}
| ^^^^^^^^
| ^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

Expand Down
92 changes: 46 additions & 46 deletions tests/ui/consts/fn_trait_refs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,168 +11,168 @@ LL | #![feature(const_cmp)]
| ^^^^^^^^^

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:13:15
--> $DIR/fn_trait_refs.rs:13:8
|
LL | T: ~const Fn<()> + ~const Destruct,
| ^^^^^^
| ^^^^^^

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:13:31
--> $DIR/fn_trait_refs.rs:13:24
|
LL | T: ~const Fn<()> + ~const Destruct,
| ^^^^^^^^
| ^^^^^^

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:13:15
--> $DIR/fn_trait_refs.rs:13:8
|
LL | T: ~const Fn<()> + ~const Destruct,
| ^^^^^^
| ^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:13:15
--> $DIR/fn_trait_refs.rs:13:8
|
LL | T: ~const Fn<()> + ~const Destruct,
| ^^^^^^
| ^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:13:31
--> $DIR/fn_trait_refs.rs:13:24
|
LL | T: ~const Fn<()> + ~const Destruct,
| ^^^^^^^^
| ^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:20:15
--> $DIR/fn_trait_refs.rs:20:8
|
LL | T: ~const FnMut<()> + ~const Destruct,
| ^^^^^^^^^
| ^^^^^^

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:20:34
--> $DIR/fn_trait_refs.rs:20:27
|
LL | T: ~const FnMut<()> + ~const Destruct,
| ^^^^^^^^
| ^^^^^^

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:20:15
--> $DIR/fn_trait_refs.rs:20:8
|
LL | T: ~const FnMut<()> + ~const Destruct,
| ^^^^^^^^^
| ^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:20:15
--> $DIR/fn_trait_refs.rs:20:8
|
LL | T: ~const FnMut<()> + ~const Destruct,
| ^^^^^^^^^
| ^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:20:34
--> $DIR/fn_trait_refs.rs:20:27
|
LL | T: ~const FnMut<()> + ~const Destruct,
| ^^^^^^^^
| ^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:27:15
--> $DIR/fn_trait_refs.rs:27:8
|
LL | T: ~const FnOnce<()>,
| ^^^^^^^^^^
| ^^^^^^

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:27:15
--> $DIR/fn_trait_refs.rs:27:8
|
LL | T: ~const FnOnce<()>,
| ^^^^^^^^^^
| ^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:27:15
--> $DIR/fn_trait_refs.rs:27:8
|
LL | T: ~const FnOnce<()>,
| ^^^^^^^^^^
| ^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:34:15
--> $DIR/fn_trait_refs.rs:34:8
|
LL | T: ~const Fn<()> + ~const Destruct,
| ^^^^^^
| ^^^^^^

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:34:31
--> $DIR/fn_trait_refs.rs:34:24
|
LL | T: ~const Fn<()> + ~const Destruct,
| ^^^^^^^^
| ^^^^^^

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:34:15
--> $DIR/fn_trait_refs.rs:34:8
|
LL | T: ~const Fn<()> + ~const Destruct,
| ^^^^^^
| ^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:34:15
--> $DIR/fn_trait_refs.rs:34:8
|
LL | T: ~const Fn<()> + ~const Destruct,
| ^^^^^^
| ^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:34:31
--> $DIR/fn_trait_refs.rs:34:24
|
LL | T: ~const Fn<()> + ~const Destruct,
| ^^^^^^^^
| ^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:48:15
--> $DIR/fn_trait_refs.rs:48:8
|
LL | T: ~const FnMut<()> + ~const Destruct,
| ^^^^^^^^^
| ^^^^^^

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:48:34
--> $DIR/fn_trait_refs.rs:48:27
|
LL | T: ~const FnMut<()> + ~const Destruct,
| ^^^^^^^^
| ^^^^^^

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:48:15
--> $DIR/fn_trait_refs.rs:48:8
|
LL | T: ~const FnMut<()> + ~const Destruct,
| ^^^^^^^^^
| ^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:48:15
--> $DIR/fn_trait_refs.rs:48:8
|
LL | T: ~const FnMut<()> + ~const Destruct,
| ^^^^^^^^^
| ^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:48:34
--> $DIR/fn_trait_refs.rs:48:27
|
LL | T: ~const FnMut<()> + ~const Destruct,
| ^^^^^^^^
| ^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

Expand Down
8 changes: 4 additions & 4 deletions tests/ui/consts/unstable-const-fn-in-libcore.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/unstable-const-fn-in-libcore.rs:19:39
--> $DIR/unstable-const-fn-in-libcore.rs:19:32
|
LL | const fn unwrap_or_else<F: ~const FnOnce() -> T>(self, f: F) -> T {
| ^^^^^^^^^^^^^
| ^^^^^^

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/unstable-const-fn-in-libcore.rs:19:39
--> $DIR/unstable-const-fn-in-libcore.rs:19:32
|
LL | const fn unwrap_or_else<F: ~const FnOnce() -> T>(self, f: F) -> T {
| ^^^^^^^^^^^^^
| ^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

Expand Down
16 changes: 8 additions & 8 deletions tests/ui/impl-trait/normalize-tait-in-const.stderr
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/normalize-tait-in-const.rs:26:42
--> $DIR/normalize-tait-in-const.rs:26:35
|
LL | const fn with_positive<F: for<'a> ~const Fn(&'a Alias<'a>) + ~const Destruct>(fun: F) {
| ^^^^^^^^^^^^^^^^^
| ^^^^^^

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/normalize-tait-in-const.rs:26:69
--> $DIR/normalize-tait-in-const.rs:26:62
|
LL | const fn with_positive<F: for<'a> ~const Fn(&'a Alias<'a>) + ~const Destruct>(fun: F) {
| ^^^^^^^^
| ^^^^^^

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/normalize-tait-in-const.rs:26:42
--> $DIR/normalize-tait-in-const.rs:26:35
|
LL | const fn with_positive<F: for<'a> ~const Fn(&'a Alias<'a>) + ~const Destruct>(fun: F) {
| ^^^^^^^^^^^^^^^^^
| ^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: `~const` can only be applied to `#[const_trait]` traits
--> $DIR/normalize-tait-in-const.rs:26:69
--> $DIR/normalize-tait-in-const.rs:26:62
|
LL | const fn with_positive<F: for<'a> ~const Fn(&'a Alias<'a>) + ~const Destruct>(fun: F) {
| ^^^^^^^^
| ^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

Expand Down
Loading

0 comments on commit a9ee1d0

Please sign in to comment.