Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
26cfeb1
Silence errors in experssions caused by bare traits in paths
estebank May 30, 2024
936b692
Silence unsized types errors in some expressions
estebank May 31, 2024
5028c30
Avoid potential for ICE by not stashing delayed as bug error and sile…
estebank May 31, 2024
f1f9842
Do not silence some sized errors as they would cause ICEs
estebank Jun 3, 2024
5441f71
Add more tracking for the HIR source of well-formedness requirement
estebank Jun 3, 2024
9a8f343
Add tests for various `dyn Trait` in path cases
estebank Jun 3, 2024
9ada8bc
Add more HIR tracking information to `ObligationCauseCode`
estebank Jun 3, 2024
5d85ffa
On object safety errors, point at specific object unsafe trait in path
estebank Jun 3, 2024
e2f2151
Move `FnCtxt::get_fn_decl` to `TyCtxt`
estebank Jun 3, 2024
230a787
Deduplicate more E0038 object safety errors by pointing at type more …
estebank Jun 3, 2024
e55ab93
Move suggestions out of main error logic
estebank Jun 8, 2024
c9e8029
Tweak wording and only suggest `dyn Trait` if `Trait` is object safe
estebank Jun 8, 2024
adc750f
Suggest `<_ as Default>::default()` when given `<Default>::default()`
estebank Jun 8, 2024
dd7b36f
review comment: `guard` -> `guar`
estebank Jun 8, 2024
e6d9050
review comment: unnecessary check for object safety on sized trait
estebank Jun 8, 2024
5f22c02
Add explanatory comment
estebank Jun 8, 2024
84be44b
Fix tidy
estebank Jun 10, 2024
d8f5b14
Remove conditional delay_as_bug from "missing `dyn` error"
estebank Jun 10, 2024
8ee6812
Remove `delay_as_bug` for fn call with unsized `Self` that return `Se…
estebank Jun 10, 2024
8140dcd
Emit method not found error for object safe traits without `dyn`
estebank Jun 10, 2024
f116d4d
Move tests that no longer ICE
estebank Jun 10, 2024
aec9eeb
Do not produce incorrect `dyn Trait` suggestion in `fn`
estebank Jun 10, 2024
05bffa0
Mention associated type with same name as trait in `E0782`
estebank Jun 11, 2024
88ccfa4
Account for `static` and `const` in suggestion
estebank Jun 11, 2024
a62bfcd
Use verbose format for fully-qualified path suggestion
estebank Jun 11, 2024
7f923f3
Fix tidy
estebank Jun 11, 2024
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
Prev Previous commit
Next Next commit
Add more tracking for the HIR source of well-formedness requirement
  • Loading branch information
estebank committed Jun 9, 2024
commit 5441f7187a1b49f3cc972a6abd2b39ec02794f2d
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/check/wfcheck.rs
Copy link
Member

@fmease fmease Jun 6, 2024

Choose a reason for hiding this comment

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

(note to self): I haven't properly reviewed commit "Add more tracking for HIR source well-formedness requirement" yet.

Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,7 @@ fn check_item_type(
traits::ObligationCause::new(
ty_span,
wfcx.body_def_id,
ObligationCauseCode::WellFormed(None),
ObligationCauseCode::WellFormed(Some(WellFormedLoc::Ty(item_id))),
),
wfcx.param_env,
item_ty,
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_hir_analysis/src/hir_wf_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fn diagnostic_hir_wf_check<'tcx>(
let def_id = match loc {
WellFormedLoc::Ty(def_id) => def_id,
WellFormedLoc::Param { function, param_idx: _ } => function,
WellFormedLoc::Expr(_) => return None,
};
let hir_id = tcx.local_def_id_to_hir_id(def_id);

Expand Down Expand Up @@ -79,7 +80,9 @@ fn diagnostic_hir_wf_check<'tcx>(
let cause = traits::ObligationCause::new(
ty.span,
self.def_id,
traits::ObligationCauseCode::WellFormed(None),
traits::ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Expr(
ty.hir_id,
))),
);

ocx.register_obligation(traits::Obligation::new(
Expand Down Expand Up @@ -191,6 +194,7 @@ fn diagnostic_hir_wf_check<'tcx>(
vec![&fn_decl.inputs[param_idx as usize]]
}
}
WellFormedLoc::Expr(_) => return None,
};
for ty in tys {
visitor.visit_ty(ty);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.register_wf_obligation(
output.into(),
call_expr.span,
ObligationCauseCode::WellFormed(None),
ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Expr(call_expr.hir_id))),
);

output
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

let ty = Ty::new_array_with_const_len(tcx, t, count);

self.register_wf_obligation(ty.into(), expr.span, ObligationCauseCode::WellFormed(None));
self.register_wf_obligation(
ty.into(),
expr.span,
ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Expr(expr.hir_id))),
);

ty
}
Expand Down
18 changes: 13 additions & 5 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

pub fn lower_ty(&self, hir_ty: &hir::Ty<'tcx>) -> LoweredTy<'tcx> {
let ty = self.lowerer().lower_ty(hir_ty);
self.register_wf_obligation(ty.into(), hir_ty.span, ObligationCauseCode::WellFormed(None));
self.register_wf_obligation(
ty.into(),
hir_ty.span,
ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Expr(hir_ty.hir_id))),
);
LoweredTy::from_raw(self, hir_ty.span, ty)
}

Expand Down Expand Up @@ -516,7 +520,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
for arg in args.iter().filter(|arg| {
matches!(arg.unpack(), GenericArgKind::Type(..) | GenericArgKind::Const(..))
}) {
self.register_wf_obligation(arg, expr.span, ObligationCauseCode::WellFormed(None));
self.register_wf_obligation(
arg,
expr.span,
ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Expr(expr.hir_id))),
);
}
}

Expand Down Expand Up @@ -770,7 +778,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.register_wf_obligation(
ty.raw.into(),
qself.span,
ObligationCauseCode::WellFormed(None),
ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Expr(hir_id))),
);
// Return directly on cache hit. This is useful to avoid doubly reporting
// errors with default match binding modes. See #44614.
Expand Down Expand Up @@ -810,7 +818,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.register_wf_obligation(
ty.raw.into(),
qself.span,
ObligationCauseCode::WellFormed(None),
ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Expr(hir_id))),
);
}

Expand Down Expand Up @@ -844,7 +852,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.register_wf_obligation(
ty.raw.into(),
qself.span,
ObligationCauseCode::WellFormed(None),
ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Expr(hir_id))),
);
}

Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_hir_typeck/src/method/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,13 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
// the function type must also be well-formed (this is not
// implied by the args being well-formed because of inherent
// impls and late-bound regions - see issue #28609).
self.register_wf_obligation(fty.into(), self.span, ObligationCauseCode::WellFormed(None));
self.register_wf_obligation(
fty.into(),
self.span,
ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Expr(
self.call_expr.hir_id,
))),
);
}

///////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_middle/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,8 @@ pub enum WellFormedLoc {
/// being the last 'parameter'
param_idx: usize,
},
/// The HIR node for the expression that introduced this obligation.
Expr(HirId),
}

#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -870,8 +870,14 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}

ty::PredicateKind::ObjectSafe(trait_def_id) => {
let mut hir_id = None;
if let ObligationCauseCode::WellFormed(Some(
crate::traits::WellFormedLoc::Expr(id),
)) = root_obligation.cause.code() {
hir_id = Some(*id);
}
let violations = self.tcx.object_safety_violations(trait_def_id);
report_object_safety_error(self.tcx, span, None, trait_def_id, violations)
report_object_safety_error(self.tcx, span, hir_id, trait_def_id, violations)
}

ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(ty)) => {
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/consts/const_refs_to_static-ice-121413.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ const REF_INTERIOR_MUT: &usize = {
static FOO: Sync = AtomicUsize::new(0);
//~^ ERROR failed to resolve: use of undeclared type `AtomicUsize`
//~| WARN trait objects without an explicit `dyn` are deprecated
//~| WARN trait objects without an explicit `dyn` are deprecated
//~| ERROR the size for values of type `(dyn Sync + 'static)` cannot be known at compilation time
//~| ERROR the size for values of type `(dyn Sync + 'static)` cannot be known at compilation time
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
unsafe { &*(&FOO as *const _ as *const usize) }
};
pub fn main() {}
16 changes: 15 additions & 1 deletion tests/ui/consts/const_refs_to_static-ice-121413.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ help: if this is an object-safe trait, use `dyn`
LL | static FOO: dyn Sync = AtomicUsize::new(0);
| +++

warning: trait objects without an explicit `dyn` are deprecated
--> $DIR/const_refs_to_static-ice-121413.rs:8:17
|
LL | static FOO: Sync = AtomicUsize::new(0);
| ^^^^
|
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
help: if this is an object-safe trait, use `dyn`
|
LL | static FOO: dyn Sync = AtomicUsize::new(0);
| +++

error[E0277]: the size for values of type `(dyn Sync + 'static)` cannot be known at compilation time
--> $DIR/const_refs_to_static-ice-121413.rs:8:17
|
Expand All @@ -40,7 +54,7 @@ LL | static FOO: Sync = AtomicUsize::new(0);
= help: the trait `Sized` is not implemented for `(dyn Sync + 'static)`
= note: constant expressions must have a statically known size

error: aborting due to 3 previous errors; 1 warning emitted
error: aborting due to 3 previous errors; 2 warnings emitted

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