Skip to content

Add try_instantiate to EarlyBinder for fallible instantiation#153904

Closed
ViewWay wants to merge 1 commit intorust-lang:mainfrom
ViewWay:feature/try-instantiate
Closed

Add try_instantiate to EarlyBinder for fallible instantiation#153904
ViewWay wants to merge 1 commit intorust-lang:mainfrom
ViewWay:feature/try-instantiate

Conversation

@ViewWay
Copy link

@ViewWay ViewWay commented Mar 15, 2026

This adds a fallible version of EarlyBinder::instantiate that returns a Result instead of panicking when parameter instantiation fails.

Motivation

The existing instantiate method panics when:

  • A parameter index is out of range of the provided args
  • The wrong kind of argument is provided (e.g., const when type expected)
  • The value has parameters but no args were provided

In error recovery paths, we want to continue compilation and report the actual error to the user instead of ICE-ing.

Changes

  • Add EarlyBinder::try_instantiate - returns Result<T, InstantiationError<I>>
  • Add InstantiationError enum with detailed error variants:
    • TypeParamOutOfRange / ConstParamOutOfRange / RegionParamOutOfRange
    • TypeParamExpected / ConstParamExpected / RegionParamExpected
    • MissingArgs
  • Add TryArgFolder implementing FallibleTypeFolder

This infrastructure can be used by callers that need graceful error handling during instantiation.

@rustbot
Copy link
Collaborator

rustbot commented Mar 15, 2026

HIR ty lowering was modified

cc @fmease

Some changes occurred in compiler/rustc_sanitizers

cc @rcvalle

@rustbot rustbot added the PG-exploit-mitigations Project group: Exploit mitigations label Mar 15, 2026
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 15, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 15, 2026

r? @JonathanBrouwer

rustbot has assigned @JonathanBrouwer.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, types
  • compiler, types expanded to 69 candidates
  • Random selection from 15 candidates

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@fmease fmease left a comment

Choose a reason for hiding this comment

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

For the love of god, please drop your cherry-pick of the commit I authored in open PR RUST-153738. It makes no sense whatsoever to take ownership of it & to include it here and in your ~5 other PRs. Your PRs can't proceed otherwise.

View changes since this review

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 15, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 15, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

This adds a fallible version of EarlyBinder::instantiate that returns
a Result instead of panicking when parameter instantiation fails.

The new try_instantiate method and InstantiationError enum allow callers
to gracefully handle cases where:
- Type/const/region parameters are out of range
- Wrong kind of argument is provided (e.g., const when type expected)
- No args provided but the value has parameters

This is useful in error recovery paths where we want to continue
compilation and report the actual error instead of ICE-ing.
@rustbot
Copy link
Collaborator

rustbot commented Mar 15, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rust-log-analyzer
Copy link
Collaborator

The job tidy failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
-        param: I::ParamTy,
-        param_index: usize,
-        args_len: usize,
-    },
+    TypeParamOutOfRange { param: I::ParamTy, param_index: usize, args_len: usize },
     /// A const parameter was referenced that is not in the provided args.
-    ConstParamOutOfRange {
-        param: I::ParamConst,
-        param_index: usize,
-        args_len: usize,
-    },
+    ConstParamOutOfRange { param: I::ParamConst, param_index: usize, args_len: usize },
     /// A region parameter was referenced that is not in the provided args.
-    RegionParamOutOfRange {
-        param_index: usize,
-        args_len: usize,
-    },
+    RegionParamOutOfRange { param_index: usize, args_len: usize },
     /// Expected a type but found a different kind.
-    TypeParamExpected {
-        param: I::ParamTy,
-        found: ty::GenericArgKind<I>,
-    },
+    TypeParamExpected { param: I::ParamTy, found: ty::GenericArgKind<I> },
     /// Expected a const but found a different kind.
-    ConstParamExpected {
-        param: I::ParamConst,
-        found: ty::GenericArgKind<I>,
-    },
+    ConstParamExpected { param: I::ParamConst, found: ty::GenericArgKind<I> },
     /// Expected a region but found a different kind.
-    RegionParamExpected {
-        param_index: usize,
-        found: ty::GenericArgKind<I>,
-    },
+    RegionParamExpected { param_index: usize, found: ty::GenericArgKind<I> },
     /// No args were provided but the value has parameters.
     MissingArgs,
 }
Diff in /checkout/compiler/rustc_type_ir/src/binder.rs:853:
     }
 
     fn try_fold_predicate(&mut self, p: I::Predicate) -> Result<I::Predicate, Self::Error> {
-        if p.has_param() {
-            p.try_super_fold_with(self)
-        } else {
-            Ok(p)
-        }
+        if p.has_param() { p.try_super_fold_with(self) } else { Ok(p) }
     }
 
     fn try_fold_clauses(&mut self, c: I::Clauses) -> Result<I::Clauses, Self::Error> {
Diff in /checkout/compiler/rustc_type_ir/src/binder.rs:864:
-        if c.has_param() {
-            c.try_super_fold_with(self)
-        } else {
-            Ok(c)
-        }
+        if c.has_param() { c.try_super_fold_with(self) } else { Ok(c) }
     }
 }
 
Diff in /checkout/compiler/rustc_type_ir/src/binder.rs:872:
 impl<'a, I: Interner> TryArgFolder<'a, I> {
-    fn ty_for_param(&self, p: I::ParamTy, _source_ty: I::Ty) -> Result<I::Ty, InstantiationError<I>> {
+    fn ty_for_param(
+        &self,
+        p: I::ParamTy,
+        _source_ty: I::Ty,
+    ) -> Result<I::Ty, InstantiationError<I>> {
         let opt_ty = self.args.get(p.index() as usize).map(|arg| arg.kind());
         let ty = match opt_ty {
             Some(ty::GenericArgKind::Type(ty)) => ty,
Diff in /checkout/compiler/rustc_type_ir/src/binder.rs:877:
             Some(kind) => {
-                return Err(InstantiationError::TypeParamExpected {
-                    param: p,
-                    found: kind,
-                })
+                return Err(InstantiationError::TypeParamExpected { param: p, found: kind });
             }
             None => {
                 return Err(InstantiationError::TypeParamOutOfRange {
Diff in /checkout/compiler/rustc_type_ir/src/binder.rs:885:
                     param: p,
                     param_index: p.index() as usize,
                     args_len: self.args.len(),
-                })
+                });
             }
         };
 
Diff in /checkout/compiler/rustc_type_ir/src/binder.rs:901:
         let ct = match opt_ct {
             Some(ty::GenericArgKind::Const(ct)) => ct,
             Some(kind) => {
-                return Err(InstantiationError::ConstParamExpected {
-                    param: p,
-                    found: kind,
-                })
+                return Err(InstantiationError::ConstParamExpected { param: p, found: kind });
             }
             None => {
                 return Err(InstantiationError::ConstParamOutOfRange {
Diff in /checkout/compiler/rustc_type_ir/src/binder.rs:911:
                     param: p,
                     param_index: p.index() as usize,
                     args_len: self.args.len(),
-                })
+                });
             }
         };
 

@oli-obk oli-obk closed this Mar 15, 2026
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Mar 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PG-exploit-mitigations Project group: Exploit mitigations T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants