Skip to content

Allow for a missing adt_def in NamePrivacyVisitor. #121482

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

Merged
merged 1 commit into from
Feb 23, 2024
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
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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

how do we ge an exprkind struct in the example you gave?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The range somehow becomes a struct. Here's partial output with -Zunpretty=hir-tree:

Expr {
    hir_id: HirId(DefId(0:3 ~ unreachable_issue_121455[470e]::test).8),
    kind: Struct(
        LangItem(
            Range,
            tests/ui/privacy/unreachable-issue-121455.rs:3:14: 3:18 (#0),
        ),  
        [   
            ExprField {
                ... 
            },  
            ExprField {
                ... 
            },  
        ],  
        None,
    ),  
    span: tests/ui/privacy/unreachable-issue-121455.rs:3:14: 3:18 (#0),
},  

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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

is this error case only reachable for ty::Error? If so, I'm wondering if we should return Resut<Option<AdtDef>, ErrorGuaranteed>. The churn may be useless, or it exposes more such cases

Copy link
Contributor Author

@nnethercote nnethercote Feb 23, 2024

Choose a reason for hiding this comment

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

I checked, and yes, the result of self.typeck_results().expr_ty(expr) is ty::Error. I don't know if that's the only thing that can reach here; it's the only thing we've seen reach here because this is the first time the the adt_def has ever failed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As for ty_adt_def's return type... it has ~100 call sites. I looked through some of them, I don't see any patterns where having an ErrorGuaranteed would be useful. There were a couple of places where a normal error (not a span_delayed_bug) was emitted in the case where it returned None.

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
6 changes: 6 additions & 0 deletions tests/ui/privacy/unreachable-issue-121455.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn test(s: &Self::Id) {
//~^ ERROR failed to resolve: `Self` is only available in impls, traits, and type definitions
match &s[0..3] {}
}

fn main() {}
9 changes: 9 additions & 0 deletions tests/ui/privacy/unreachable-issue-121455.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0433]: failed to resolve: `Self` is only available in impls, traits, and type definitions
--> $DIR/unreachable-issue-121455.rs:1:13
|
LL | fn test(s: &Self::Id) {
| ^^^^ `Self` is only available in impls, traits, and type definitions

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0433`.