Skip to content

Commit

Permalink
Auto merge of #121491 - matthiaskrgr:rollup-wkzqawy, r=matthiaskrgr
Browse files Browse the repository at this point in the history
Rollup of 8 pull requests

Successful merges:

 - #121434 (Fix #121208 fallout)
 - #121471 (When encountering `<&T as Clone>::clone(x)` because `T: Clone`, suggest `#[derive(Clone)]`)
 - #121476 (remove `llvm.assertions=true` in compiler profile)
 - #121479 (fix generalizer unsoundness)
 - #121480 (Fix more #121208 fallout)
 - #121482 (Allow for a missing `adt_def` in `NamePrivacyVisitor`.)
 - #121484 (coverage: Use variable name `this` in `CoverageGraph::from_mir`)
 - #121487 (Explicitly call `emit_stashed_diagnostics`.)

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Feb 23, 2024
2 parents ea2cc43 + 6ee43bc commit 52cea08
Show file tree
Hide file tree
Showing 34 changed files with 377 additions and 133 deletions.
4 changes: 3 additions & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1636,7 +1636,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
if let Some(old_def_id) = self.orig_opt_local_def_id(param) {
old_def_id
} else {
self.dcx().span_bug(lifetime.ident.span, "no def-id for fresh lifetime");
self.dcx()
.span_delayed_bug(lifetime.ident.span, "no def-id for fresh lifetime");
continue;
}
}

Expand Down
14 changes: 9 additions & 5 deletions compiler/rustc_borrowck/src/type_check/relate_tys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ impl<'me, 'bccx, 'tcx> NllTypeRelating<'me, 'bccx, 'tcx> {
// `handle_opaque_type` cannot handle subtyping, so to support subtyping
// we instead eagerly generalize here. This is a bit of a mess but will go
// away once we're using the new solver.
let mut enable_subtyping = |ty, ty_is_expected| {
//
// Given `opaque rel B`, we create a new infer var `ty_vid` constrain it
// by using `ty_vid rel B` and then finally and end by equating `ty_vid` to
// the opaque.
let mut enable_subtyping = |ty, opaque_is_expected| {
let ty_vid = infcx.next_ty_var_id_in_universe(
TypeVariableOrigin {
kind: TypeVariableOriginKind::MiscVariable,
Expand All @@ -132,15 +136,15 @@ impl<'me, 'bccx, 'tcx> NllTypeRelating<'me, 'bccx, 'tcx> {
ty::UniverseIndex::ROOT,
);

let variance = if ty_is_expected {
let variance = if opaque_is_expected {
self.ambient_variance
} else {
self.ambient_variance.xform(ty::Contravariant)
};

self.type_checker.infcx.instantiate_ty_var(
self,
ty_is_expected,
opaque_is_expected,
ty_vid,
variance,
ty,
Expand All @@ -149,8 +153,8 @@ impl<'me, 'bccx, 'tcx> NllTypeRelating<'me, 'bccx, 'tcx> {
};

let (a, b) = match (a.kind(), b.kind()) {
(&ty::Alias(ty::Opaque, ..), _) => (a, enable_subtyping(b, false)?),
(_, &ty::Alias(ty::Opaque, ..)) => (enable_subtyping(a, true)?, b),
(&ty::Alias(ty::Opaque, ..), _) => (a, enable_subtyping(b, true)?),
(_, &ty::Alias(ty::Opaque, ..)) => (enable_subtyping(a, false)?, b),
_ => unreachable!(
"expected at least one opaque type in `relate_opaques`, got {a} and {b}."
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,17 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
trait_m_sig.inputs_and_output,
));
if !ocx.select_all_or_error().is_empty() {
// This code path is not reached in any tests, but may be reachable. If
// this is triggered, it should be converted to `delayed_bug` and the
// triggering case turned into a test.
tcx.dcx().bug("encountered errors when checking RPITIT refinement (selection)");
tcx.dcx().delayed_bug("encountered errors when checking RPITIT refinement (selection)");
return;
}
let outlives_env = OutlivesEnvironment::with_bounds(
param_env,
infcx.implied_bounds_tys(param_env, impl_m.def_id.expect_local(), &implied_wf_types),
);
let errors = infcx.resolve_regions(&outlives_env);
if !errors.is_empty() {
// This code path is not reached in any tests, but may be reachable. If
// this is triggered, it should be converted to `delayed_bug` and the
// triggering case turned into a test.
tcx.dcx().bug("encountered errors when checking RPITIT refinement (regions)");
tcx.dcx().delayed_bug("encountered errors when checking RPITIT refinement (regions)");
return;
}
// Resolve any lifetime variables that may have been introduced during normalization.
let Ok((trait_bounds, impl_bounds)) = infcx.fully_resolve((trait_bounds, impl_bounds)) else {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/collect/generics_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {

if is_host_effect {
if let Some(idx) = host_effect_index {
tcx.dcx().span_bug(
tcx.dcx().span_delayed_bug(
param.span,
format!("parent also has host effect param? index: {idx}, def: {def_id:?}"),
);
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// coercions from ! to `expected`.
if ty.is_never() {
if let Some(_) = self.typeck_results.borrow().adjustments().get(expr.hir_id) {
self.dcx()
.span_bug(expr.span, "expression with never type wound up being adjusted");
let reported = self.dcx().span_delayed_bug(
expr.span,
"expression with never type wound up being adjusted",
);
return Ty::new_error(self.tcx(), reported);
}

let adj_ty = self.next_ty_var(TypeVariableOrigin {
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir_typeck/src/mem_categorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,8 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
match ty.kind() {
ty::Tuple(args) => Ok(args.len()),
_ => {
self.tcx().dcx().span_bug(span, "tuple pattern not applied to a tuple");
self.tcx().dcx().span_delayed_bug(span, "tuple pattern not applied to a tuple");
Err(())
}
}
}
Expand Down
42 changes: 24 additions & 18 deletions compiler/rustc_infer/src/infer/relate/generalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ impl<'tcx> InferCtxt<'tcx> {
/// This is *not* expected to be used anywhere except for an implementation of
/// `TypeRelation`. Do not use this, and instead please use `At::eq`, for all
/// other usecases (i.e. setting the value of a type var).
#[instrument(level = "debug", skip(self, relation, target_is_expected))]
#[instrument(level = "debug", skip(self, relation))]
pub fn instantiate_ty_var<R: ObligationEmittingRelation<'tcx>>(
&self,
relation: &mut R,
target_is_expected: bool,
target_vid: ty::TyVid,
ambient_variance: ty::Variance,
instantiation_variance: ty::Variance,
source_ty: Ty<'tcx>,
) -> RelateResult<'tcx, ()> {
debug_assert!(self.inner.borrow_mut().type_variables().probe(target_vid).is_unknown());
Expand All @@ -46,7 +46,7 @@ impl<'tcx> InferCtxt<'tcx> {
//
// We then relate `generalized_ty <: source_ty`,adding constraints like `'x: '?2` and `?1 <: ?3`.
let Generalization { value_may_be_infer: generalized_ty, has_unconstrained_ty_var } =
self.generalize(relation.span(), target_vid, ambient_variance, source_ty)?;
self.generalize(relation.span(), target_vid, instantiation_variance, source_ty)?;

// Constrain `b_vid` to the generalized type `generalized_ty`.
if let &ty::Infer(ty::TyVar(generalized_vid)) = generalized_ty.kind() {
Expand All @@ -73,7 +73,7 @@ impl<'tcx> InferCtxt<'tcx> {
// the alias can be normalized to something which does not
// mention `?0`.
if self.next_trait_solver() {
let (lhs, rhs, direction) = match ambient_variance {
let (lhs, rhs, direction) = match instantiation_variance {
ty::Variance::Invariant => {
(generalized_ty.into(), source_ty.into(), AliasRelationDirection::Equate)
}
Expand Down Expand Up @@ -106,22 +106,28 @@ impl<'tcx> InferCtxt<'tcx> {
}
}
} else {
// HACK: make sure that we `a_is_expected` continues to be
// correct when relating the generalized type with the source.
// NOTE: The `instantiation_variance` is not the same variance as
// used by the relation. When instantiating `b`, `target_is_expected`
// is flipped and the `instantion_variance` is also flipped. To
// constrain the `generalized_ty` while using the original relation,
// we therefore only have to flip the arguments.
//
// ```ignore (not code)
// ?a rel B
// instantiate_ty_var(?a, B) # expected and variance not flipped
// B' rel B
// ```
// or
// ```ignore (not code)
// A rel ?b
// instantiate_ty_var(?b, A) # expected and variance flipped
// A rel A'
// ```
if target_is_expected == relation.a_is_expected() {
relation.relate_with_variance(
ambient_variance,
ty::VarianceDiagInfo::default(),
generalized_ty,
source_ty,
)?;
relation.relate(generalized_ty, source_ty)?;
} else {
relation.relate_with_variance(
ambient_variance.xform(ty::Contravariant),
ty::VarianceDiagInfo::default(),
source_ty,
generalized_ty,
)?;
debug!("flip relation");
relation.relate(source_ty, generalized_ty)?;
}
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ lint_non_upper_case_global = {$sort} `{$name}` should have an upper case name
lint_noop_method_call = call to `.{$method}()` on a reference in this situation does nothing
.suggestion = remove this redundant call
.note = the type `{$orig_ty}` does not implement `{$trait_}`, so calling `{$method}` on `&{$orig_ty}` copies the reference, which does not do anything and can be removed
.derive_suggestion = if you meant to clone `{$orig_ty}`, implement `Clone` for it
lint_only_cast_u8_to_char = only `u8` can be cast into `char`
.suggestion = use a `char` literal instead
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,12 @@ pub struct NoopMethodCallDiag<'a> {
pub trait_: Symbol,
#[suggestion(code = "", applicability = "machine-applicable")]
pub label: Span,
#[suggestion(
lint_derive_suggestion,
code = "#[derive(Clone)]\n",
applicability = "maybe-incorrect"
)]
pub suggest_derive: Option<Span>,
}

#[derive(LintDiagnostic)]
Expand Down
12 changes: 11 additions & 1 deletion compiler/rustc_lint/src/noop_method_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,20 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
let orig_ty = expr_ty.peel_refs();

if receiver_ty == expr_ty {
let suggest_derive = match orig_ty.kind() {
ty::Adt(def, _) => Some(cx.tcx.def_span(def.did()).shrink_to_lo()),
_ => None,
};
cx.emit_span_lint(
NOOP_METHOD_CALL,
span,
NoopMethodCallDiag { method: call.ident.name, orig_ty, trait_, label: span },
NoopMethodCallDiag {
method: call.ident.name,
orig_ty,
trait_,
label: span,
suggest_derive,
},
);
} else {
match name {
Expand Down
13 changes: 6 additions & 7 deletions compiler/rustc_mir_transform/src/coverage/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,18 @@ impl CoverageGraph {
}
}

let mut basic_coverage_blocks =
Self { bcbs, bb_to_bcb, successors, predecessors, dominators: None };
let dominators = dominators::dominators(&basic_coverage_blocks);
basic_coverage_blocks.dominators = Some(dominators);
let mut this = Self { bcbs, bb_to_bcb, successors, predecessors, dominators: None };

this.dominators = Some(dominators::dominators(&this));

// The coverage graph's entry-point node (bcb0) always starts with bb0,
// which never has predecessors. Any other blocks merged into bcb0 can't
// have multiple (coverage-relevant) predecessors, so bcb0 always has
// zero in-edges.
assert!(basic_coverage_blocks[START_BCB].leader_bb() == mir::START_BLOCK);
assert!(basic_coverage_blocks.predecessors[START_BCB].is_empty());
assert!(this[START_BCB].leader_bb() == mir::START_BLOCK);
assert!(this.predecessors[START_BCB].is_empty());

basic_coverage_blocks
this
}

fn compute_basic_coverage_blocks(
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_privacy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,10 @@ impl<'tcx> Visitor<'tcx> for NamePrivacyVisitor<'tcx> {
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
if let hir::ExprKind::Struct(qpath, fields, ref base) = expr.kind {
let res = self.typeck_results().qpath_res(qpath, expr.hir_id);
let adt = self.typeck_results().expr_ty(expr).ty_adt_def().unwrap();
let Some(adt) = self.typeck_results().expr_ty(expr).ty_adt_def() else {
self.tcx.dcx().span_delayed_bug(expr.span, "no adt_def for expression");
return;
};
let variant = adt.variant_of_res(res);
if let Some(base) = *base {
// If the expression uses FRU we need to make sure all the unmentioned fields
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_trait_selection/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,7 @@ fn do_normalize_predicates<'tcx>(
// the normalized predicates.
let errors = infcx.resolve_regions(&outlives_env);
if !errors.is_empty() {
// @lcnr: Let's still ICE here for now. I want a test case
// for that.
tcx.dcx().span_bug(
tcx.dcx().span_delayed_bug(
span,
format!("failed region resolution while normalizing {elaborated_env:?}: {errors:?}"),
);
Expand Down
6 changes: 3 additions & 3 deletions src/bootstrap/defaults/config.compiler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ lto = "off"
frame-pointers = true

[llvm]
# This enables debug-assertions in LLVM,
# catching logic errors in codegen much earlier in the process.
assertions = true
# Having this set to true disrupts compiler development workflows for people who use `llvm.download-ci-llvm = true`
# because we don't provide ci-llvm on the `rustc-alt-builds` server. Therefore, it is kept off by default.
assertions = false
# Enable warnings during the LLVM compilation (when LLVM is changed, causing a compilation)
enable-warnings = true
# Will download LLVM from CI if available on your platform.
Expand Down
18 changes: 13 additions & 5 deletions src/tools/rustfmt/src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,21 @@ impl<'a> Parser<'a> {
fn parse_crate_mod(&mut self) -> Result<ast::Crate, ParserError> {
let mut parser = AssertUnwindSafe(&mut self.parser);

match catch_unwind(move || parser.parse_crate_mod()) {
Ok(Ok(k)) => Ok(k),
Ok(Err(db)) => {
// rustfmt doesn't use `run_compiler` like other tools, so it must emit
// any stashed diagnostics itself, otherwise the `DiagCtxt` will assert
// when dropped. The final result here combines the parsing result and
// the `emit_stashed_diagnostics` result.
let parse_res = catch_unwind(move || parser.parse_crate_mod());
let stashed_res = self.parser.dcx().emit_stashed_diagnostics();
let err = Err(ParserError::ParsePanicError);
match (parse_res, stashed_res) {
(Ok(Ok(k)), None) => Ok(k),
(Ok(Ok(_)), Some(_guar)) => err,
(Ok(Err(db)), _) => {
db.emit();
Err(ParserError::ParseError)
err
}
Err(_) => Err(ParserError::ParsePanicError),
(Err(_), _) => err,
}
}
}
7 changes: 7 additions & 0 deletions src/tools/rustfmt/src/test/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,10 @@ fn crate_parsing_errors_on_unclosed_delims() {
let filename = "tests/parser/unclosed-delims/issue_4466.rs";
assert_parser_error(filename);
}

#[test]
fn crate_parsing_stashed_diag() {
// See also https://github.com/rust-lang/rust/issues/121450
let filename = "tests/parser/stashed-diag.rs";
assert_parser_error(filename);
}
3 changes: 3 additions & 0 deletions src/tools/rustfmt/tests/parser/stashed-diag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#![u={static N;}]

fn main() {}
18 changes: 18 additions & 0 deletions tests/ui/impl-trait/in-trait/span-bug-issue-121457.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub trait Iterable {
type Item<'a>
where
Self: 'a;

fn iter(&self) -> impl Iterator;
}

impl<'a, I: 'a + Iterable> Iterable for &'a I {
type Item = u32;
//~^ ERROR lifetime parameters or bounds on type `Item` do not match the trait declaration

fn iter(&self) -> impl for<'missing> Iterator<Item = Self::Item<'missing>> {}
//~^ ERROR binding for associated type `Item` references lifetime `'missing`
//~| ERROR `()` is not an iterator
}

fn main() {}
30 changes: 30 additions & 0 deletions tests/ui/impl-trait/in-trait/span-bug-issue-121457.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
error[E0582]: binding for associated type `Item` references lifetime `'missing`, which does not appear in the trait input types
--> $DIR/span-bug-issue-121457.rs:13:51
|
LL | fn iter(&self) -> impl for<'missing> Iterator<Item = Self::Item<'missing>> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0195]: lifetime parameters or bounds on type `Item` do not match the trait declaration
--> $DIR/span-bug-issue-121457.rs:10:14
|
LL | type Item<'a>
| ---- lifetimes in impl do not match this type in trait
LL | where
LL | Self: 'a;
| -- this bound might be missing in the impl
...
LL | type Item = u32;
| ^ lifetimes do not match type in trait

error[E0277]: `()` is not an iterator
--> $DIR/span-bug-issue-121457.rs:13:23
|
LL | fn iter(&self) -> impl for<'missing> Iterator<Item = Self::Item<'missing>> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `()` is not an iterator
|
= help: the trait `Iterator` is not implemented for `()`

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0195, E0277, E0582.
For more information about an error, try `rustc --explain E0195`.
Loading

0 comments on commit 52cea08

Please sign in to comment.