Skip to content

Comments

fix(codegen): Use body_codegen_attrs For Caller In adjust_target_feature_sig#152837

Merged
rust-bors[bot] merged 2 commits intorust-lang:mainfrom
TKanX:bugfix/152340-codegen-fn-attrs-const-caller
Feb 22, 2026
Merged

fix(codegen): Use body_codegen_attrs For Caller In adjust_target_feature_sig#152837
rust-bors[bot] merged 2 commits intorust-lang:mainfrom
TKanX:bugfix/152340-codegen-fn-attrs-const-caller

Conversation

@TKanX
Copy link
Contributor

@TKanX TKanX commented Feb 19, 2026

Summary:

Problem:

Coercing a #[target_feature] const fn to a function pointer inside a const body triggers an ICE (debug builds only):

#[target_feature(enable = "sse2")]
const fn with_target_feature() {}

const X: () = unsafe {
    let _: unsafe fn() = with_target_feature; // ICE
};
assertion failed: def_kind.has_codegen_attrs()
unexpected `def_kind` in `codegen_fn_attrs`: Const

Root Cause:

Introduced in #135504 (2025-01-14, commit 8fee6a77394). adjust_target_feature_sig unconditionally calls codegen_fn_attrs(caller) to get the caller's target features. codegen_fn_attrs requires that the DefId satisfies has_codegen_attrs(). DefKind::Const, AssocConst, and InlineConst do not — they have no codegen attributes by design. The debug assertion fires.

In release builds the call "worked" accidentally: codegen_fn_attrs on a const would reach the query machinery and happen to return empty attributes, producing a correct (but unguaranteed) result. The bug was latent until debug builds exposed it.

Solution:

Replace codegen_fn_attrs(caller) with body_codegen_attrs(caller). body_codegen_attrs exists precisely for this case: it delegates to codegen_fn_attrs for function-like DefKinds and returns CodegenFnAttrs::EMPTY for const items. A const body has no target features, so returning empty is semantically correct.

Also fix the pre-existing variable name callee_featurescaller_features (the variable holds the caller's features, not the callee's).

Changes:

  • compiler/rustc_middle/src/ty/context.rs: adjust_target_feature_sig — use body_codegen_attrs for caller; rename callee_featurescaller_features.
  • tests/ui/target-feature/const-target-feature-fn-ptr-coercion.rs: regression test covering Const, AssocConst, and InlineConst caller contexts.

Fixes #152340.

@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 Feb 19, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 19, 2026

r? @jackh726

rustbot has assigned @jackh726.
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
  • compiler expanded to 68 candidates
  • Random selection from 15 candidates

@rustbot
Copy link
Collaborator

rustbot commented Feb 19, 2026

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

@TKanX
Copy link
Contributor Author

TKanX commented Feb 20, 2026

@rustbot label +I-ICE +A-codegen +A-target-feature +C-bug +T-compiler

@rustbot rustbot added A-codegen Area: Code generation A-target-feature Area: Enabling/disabling target features like AVX, Neon, etc. C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ labels Feb 20, 2026
@TKanX
Copy link
Contributor Author

TKanX commented Feb 21, 2026

r? @makai410

@rustbot rustbot assigned makai410 and unassigned jackh726 Feb 21, 2026
@makai410
Copy link
Member

@rustbot reroll

@rustbot rustbot assigned dingxiangfei2009 and unassigned makai410 Feb 21, 2026
@dingxiangfei2009
Copy link
Contributor

@TKanX Thank you for looking into this! LGTM.

@bors r+ rollup=always

@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 21, 2026

📌 Commit 034f7f0 has been approved by dingxiangfei2009

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 21, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Feb 21, 2026
…rs-const-caller, r=dingxiangfei2009

fix(codegen): Use `body_codegen_attrs` For Caller In `adjust_target_feature_sig`

### Summary:

#### Problem:

Coercing a `#[target_feature]` `const fn` to a function pointer inside a `const` body triggers an ICE (debug builds only):

```rust
#[target_feature(enable = "sse2")]
const fn with_target_feature() {}

const X: () = unsafe {
    let _: unsafe fn() = with_target_feature; // ICE
};
```

```text
assertion failed: def_kind.has_codegen_attrs()
unexpected `def_kind` in `codegen_fn_attrs`: Const
```

#### Root Cause:

Introduced in rust-lang#135504 (2025-01-14, commit `8fee6a77394`). `adjust_target_feature_sig` unconditionally calls `codegen_fn_attrs(caller)` to get the caller's target features. `codegen_fn_attrs` requires that the `DefId` satisfies `has_codegen_attrs()`. `DefKind::Const`, `AssocConst`, and `InlineConst` do not — they have no codegen attributes by design. The debug assertion fires.

In release builds the call "worked" accidentally: `codegen_fn_attrs` on a const would reach the query machinery and happen to return empty attributes, producing a correct (but unguaranteed) result. The bug was latent until debug builds exposed it.

#### Solution:

Replace `codegen_fn_attrs(caller)` with `body_codegen_attrs(caller)`. `body_codegen_attrs` exists precisely for this case: it delegates to `codegen_fn_attrs` for function-like `DefKind`s and returns `CodegenFnAttrs::EMPTY` for const items. A const body has no target features, so returning empty is semantically correct.

Also fix the pre-existing variable name `callee_features` → `caller_features` (the variable holds the *caller*'s features, not the callee's).

### Changes:

- `compiler/rustc_middle/src/ty/context.rs`: `adjust_target_feature_sig` — use `body_codegen_attrs` for `caller`; rename `callee_features` → `caller_features`.
- `tests/ui/target-feature/const-target-feature-fn-ptr-coercion.rs`: regression test covering `Const`, `AssocConst`, and `InlineConst` caller contexts.

Fixes rust-lang#152340.
rust-bors bot pushed a commit that referenced this pull request Feb 21, 2026
Rollup of 14 pull requests

Successful merges:

 - #150468 (rustc_target: callconv: powerpc64: Use the ABI set in target options instead of guessing)
 - #151628 (Fix ICE in const eval of packed SIMD types with non-power-of-two element counts)
 - #151740 (Fix short backtraces from stripped executables)
 - #151871 (don't use env with infer vars)
 - #152591 (Simplify internals of `{Rc,Arc}::default`)
 - #152865 (Fixed ByteStr not padding within its Display trait when no specific alignment is mentioned)
 - #152908 (Enable rust.remap-debuginfo in the dist profile)
 - #152705 (Test(lib/win/proc): Skip `raw_attributes` doctest under Win7)
 - #152767 (fix typo in `carryless_mul` macro invocation)
 - #152837 (fix(codegen): Use `body_codegen_attrs` For Caller In `adjust_target_feature_sig`)
 - #152871 (Fix warnings in rs{begin,end}.rs files)
 - #152921 (Add build.rustdoc option to bootstrap config)
 - #152933 (Start migration for `LintDiagnostic` items by adding API and migrating `LinkerOutput` lint)
 - #152937 (remove unneeded reboxing)
jhpratt added a commit to jhpratt/rust that referenced this pull request Feb 22, 2026
…rs-const-caller, r=dingxiangfei2009

fix(codegen): Use `body_codegen_attrs` For Caller In `adjust_target_feature_sig`

### Summary:

#### Problem:

Coercing a `#[target_feature]` `const fn` to a function pointer inside a `const` body triggers an ICE (debug builds only):

```rust
#[target_feature(enable = "sse2")]
const fn with_target_feature() {}

const X: () = unsafe {
    let _: unsafe fn() = with_target_feature; // ICE
};
```

```text
assertion failed: def_kind.has_codegen_attrs()
unexpected `def_kind` in `codegen_fn_attrs`: Const
```

#### Root Cause:

Introduced in rust-lang#135504 (2025-01-14, commit `8fee6a77394`). `adjust_target_feature_sig` unconditionally calls `codegen_fn_attrs(caller)` to get the caller's target features. `codegen_fn_attrs` requires that the `DefId` satisfies `has_codegen_attrs()`. `DefKind::Const`, `AssocConst`, and `InlineConst` do not — they have no codegen attributes by design. The debug assertion fires.

In release builds the call "worked" accidentally: `codegen_fn_attrs` on a const would reach the query machinery and happen to return empty attributes, producing a correct (but unguaranteed) result. The bug was latent until debug builds exposed it.

#### Solution:

Replace `codegen_fn_attrs(caller)` with `body_codegen_attrs(caller)`. `body_codegen_attrs` exists precisely for this case: it delegates to `codegen_fn_attrs` for function-like `DefKind`s and returns `CodegenFnAttrs::EMPTY` for const items. A const body has no target features, so returning empty is semantically correct.

Also fix the pre-existing variable name `callee_features` → `caller_features` (the variable holds the *caller*'s features, not the callee's).

### Changes:

- `compiler/rustc_middle/src/ty/context.rs`: `adjust_target_feature_sig` — use `body_codegen_attrs` for `caller`; rename `callee_features` → `caller_features`.
- `tests/ui/target-feature/const-target-feature-fn-ptr-coercion.rs`: regression test covering `Const`, `AssocConst`, and `InlineConst` caller contexts.

Fixes rust-lang#152340.
rust-bors bot pushed a commit that referenced this pull request Feb 22, 2026
Rollup of 15 pull requests

Successful merges:

 - #149366 (GVN: consider constants of primitive types as deterministic)
 - #150468 (rustc_target: callconv: powerpc64: Use the ABI set in target options instead of guessing)
 - #151628 (Fix ICE in const eval of packed SIMD types with non-power-of-two element counts)
 - #151871 (don't use env with infer vars)
 - #152591 (Simplify internals of `{Rc,Arc}::default`)
 - #152657 (std: move `exit` out of PAL)
 - #152865 (Fixed ByteStr not padding within its Display trait when no specific alignment is mentioned)
 - #152908 (Enable rust.remap-debuginfo in the dist profile)
 - #152705 (Test(lib/win/proc): Skip `raw_attributes` doctest under Win7)
 - #152767 (fix typo in `carryless_mul` macro invocation)
 - #152837 (fix(codegen): Use `body_codegen_attrs` For Caller In `adjust_target_feature_sig`)
 - #152871 (Fix warnings in rs{begin,end}.rs files)
 - #152921 (Add build.rustdoc option to bootstrap config)
 - #152933 (Start migration for `LintDiagnostic` items by adding API and migrating `LinkerOutput` lint)
 - #152937 (remove unneeded reboxing)
jhpratt added a commit to jhpratt/rust that referenced this pull request Feb 22, 2026
…rs-const-caller, r=dingxiangfei2009

fix(codegen): Use `body_codegen_attrs` For Caller In `adjust_target_feature_sig`

### Summary:

#### Problem:

Coercing a `#[target_feature]` `const fn` to a function pointer inside a `const` body triggers an ICE (debug builds only):

```rust
#[target_feature(enable = "sse2")]
const fn with_target_feature() {}

const X: () = unsafe {
    let _: unsafe fn() = with_target_feature; // ICE
};
```

```text
assertion failed: def_kind.has_codegen_attrs()
unexpected `def_kind` in `codegen_fn_attrs`: Const
```

#### Root Cause:

Introduced in rust-lang#135504 (2025-01-14, commit `8fee6a77394`). `adjust_target_feature_sig` unconditionally calls `codegen_fn_attrs(caller)` to get the caller's target features. `codegen_fn_attrs` requires that the `DefId` satisfies `has_codegen_attrs()`. `DefKind::Const`, `AssocConst`, and `InlineConst` do not — they have no codegen attributes by design. The debug assertion fires.

In release builds the call "worked" accidentally: `codegen_fn_attrs` on a const would reach the query machinery and happen to return empty attributes, producing a correct (but unguaranteed) result. The bug was latent until debug builds exposed it.

#### Solution:

Replace `codegen_fn_attrs(caller)` with `body_codegen_attrs(caller)`. `body_codegen_attrs` exists precisely for this case: it delegates to `codegen_fn_attrs` for function-like `DefKind`s and returns `CodegenFnAttrs::EMPTY` for const items. A const body has no target features, so returning empty is semantically correct.

Also fix the pre-existing variable name `callee_features` → `caller_features` (the variable holds the *caller*'s features, not the callee's).

### Changes:

- `compiler/rustc_middle/src/ty/context.rs`: `adjust_target_feature_sig` — use `body_codegen_attrs` for `caller`; rename `callee_features` → `caller_features`.
- `tests/ui/target-feature/const-target-feature-fn-ptr-coercion.rs`: regression test covering `Const`, `AssocConst`, and `InlineConst` caller contexts.

Fixes rust-lang#152340.
rust-bors bot pushed a commit that referenced this pull request Feb 22, 2026
Rollup of 14 pull requests

Successful merges:

 - #150468 (rustc_target: callconv: powerpc64: Use the ABI set in target options instead of guessing)
 - #151628 (Fix ICE in const eval of packed SIMD types with non-power-of-two element counts)
 - #151871 (don't use env with infer vars)
 - #152591 (Simplify internals of `{Rc,Arc}::default`)
 - #152657 (std: move `exit` out of PAL)
 - #152865 (Fixed ByteStr not padding within its Display trait when no specific alignment is mentioned)
 - #152908 (Enable rust.remap-debuginfo in the dist profile)
 - #152705 (Test(lib/win/proc): Skip `raw_attributes` doctest under Win7)
 - #152767 (fix typo in `carryless_mul` macro invocation)
 - #152837 (fix(codegen): Use `body_codegen_attrs` For Caller In `adjust_target_feature_sig`)
 - #152871 (Fix warnings in rs{begin,end}.rs files)
 - #152921 (Add build.rustdoc option to bootstrap config)
 - #152933 (Start migration for `LintDiagnostic` items by adding API and migrating `LinkerOutput` lint)
 - #152937 (remove unneeded reboxing)
jhpratt added a commit to jhpratt/rust that referenced this pull request Feb 22, 2026
…rs-const-caller, r=dingxiangfei2009

fix(codegen): Use `body_codegen_attrs` For Caller In `adjust_target_feature_sig`

### Summary:

#### Problem:

Coercing a `#[target_feature]` `const fn` to a function pointer inside a `const` body triggers an ICE (debug builds only):

```rust
#[target_feature(enable = "sse2")]
const fn with_target_feature() {}

const X: () = unsafe {
    let _: unsafe fn() = with_target_feature; // ICE
};
```

```text
assertion failed: def_kind.has_codegen_attrs()
unexpected `def_kind` in `codegen_fn_attrs`: Const
```

#### Root Cause:

Introduced in rust-lang#135504 (2025-01-14, commit `8fee6a77394`). `adjust_target_feature_sig` unconditionally calls `codegen_fn_attrs(caller)` to get the caller's target features. `codegen_fn_attrs` requires that the `DefId` satisfies `has_codegen_attrs()`. `DefKind::Const`, `AssocConst`, and `InlineConst` do not — they have no codegen attributes by design. The debug assertion fires.

In release builds the call "worked" accidentally: `codegen_fn_attrs` on a const would reach the query machinery and happen to return empty attributes, producing a correct (but unguaranteed) result. The bug was latent until debug builds exposed it.

#### Solution:

Replace `codegen_fn_attrs(caller)` with `body_codegen_attrs(caller)`. `body_codegen_attrs` exists precisely for this case: it delegates to `codegen_fn_attrs` for function-like `DefKind`s and returns `CodegenFnAttrs::EMPTY` for const items. A const body has no target features, so returning empty is semantically correct.

Also fix the pre-existing variable name `callee_features` → `caller_features` (the variable holds the *caller*'s features, not the callee's).

### Changes:

- `compiler/rustc_middle/src/ty/context.rs`: `adjust_target_feature_sig` — use `body_codegen_attrs` for `caller`; rename `callee_features` → `caller_features`.
- `tests/ui/target-feature/const-target-feature-fn-ptr-coercion.rs`: regression test covering `Const`, `AssocConst`, and `InlineConst` caller contexts.

Fixes rust-lang#152340.
rust-bors bot pushed a commit that referenced this pull request Feb 22, 2026
Rollup of 13 pull requests

Successful merges:

 - #150468 (rustc_target: callconv: powerpc64: Use the ABI set in target options instead of guessing)
 - #151628 (Fix ICE in const eval of packed SIMD types with non-power-of-two element counts)
 - #151871 (don't use env with infer vars)
 - #152591 (Simplify internals of `{Rc,Arc}::default`)
 - #152865 (Fixed ByteStr not padding within its Display trait when no specific alignment is mentioned)
 - #152908 (Enable rust.remap-debuginfo in the dist profile)
 - #152705 (Test(lib/win/proc): Skip `raw_attributes` doctest under Win7)
 - #152767 (fix typo in `carryless_mul` macro invocation)
 - #152837 (fix(codegen): Use `body_codegen_attrs` For Caller In `adjust_target_feature_sig`)
 - #152871 (Fix warnings in rs{begin,end}.rs files)
 - #152921 (Add build.rustdoc option to bootstrap config)
 - #152933 (Start migration for `LintDiagnostic` items by adding API and migrating `LinkerOutput` lint)
 - #152937 (remove unneeded reboxing)
jhpratt added a commit to jhpratt/rust that referenced this pull request Feb 22, 2026
…rs-const-caller, r=dingxiangfei2009

fix(codegen): Use `body_codegen_attrs` For Caller In `adjust_target_feature_sig`

### Summary:

#### Problem:

Coercing a `#[target_feature]` `const fn` to a function pointer inside a `const` body triggers an ICE (debug builds only):

```rust
#[target_feature(enable = "sse2")]
const fn with_target_feature() {}

const X: () = unsafe {
    let _: unsafe fn() = with_target_feature; // ICE
};
```

```text
assertion failed: def_kind.has_codegen_attrs()
unexpected `def_kind` in `codegen_fn_attrs`: Const
```

#### Root Cause:

Introduced in rust-lang#135504 (2025-01-14, commit `8fee6a77394`). `adjust_target_feature_sig` unconditionally calls `codegen_fn_attrs(caller)` to get the caller's target features. `codegen_fn_attrs` requires that the `DefId` satisfies `has_codegen_attrs()`. `DefKind::Const`, `AssocConst`, and `InlineConst` do not — they have no codegen attributes by design. The debug assertion fires.

In release builds the call "worked" accidentally: `codegen_fn_attrs` on a const would reach the query machinery and happen to return empty attributes, producing a correct (but unguaranteed) result. The bug was latent until debug builds exposed it.

#### Solution:

Replace `codegen_fn_attrs(caller)` with `body_codegen_attrs(caller)`. `body_codegen_attrs` exists precisely for this case: it delegates to `codegen_fn_attrs` for function-like `DefKind`s and returns `CodegenFnAttrs::EMPTY` for const items. A const body has no target features, so returning empty is semantically correct.

Also fix the pre-existing variable name `callee_features` → `caller_features` (the variable holds the *caller*'s features, not the callee's).

### Changes:

- `compiler/rustc_middle/src/ty/context.rs`: `adjust_target_feature_sig` — use `body_codegen_attrs` for `caller`; rename `callee_features` → `caller_features`.
- `tests/ui/target-feature/const-target-feature-fn-ptr-coercion.rs`: regression test covering `Const`, `AssocConst`, and `InlineConst` caller contexts.

Fixes rust-lang#152340.
rust-bors bot pushed a commit that referenced this pull request Feb 22, 2026
Rollup of 15 pull requests

Successful merges:

 - #150468 (rustc_target: callconv: powerpc64: Use the ABI set in target options instead of guessing)
 - #151628 (Fix ICE in const eval of packed SIMD types with non-power-of-two element counts)
 - #151871 (don't use env with infer vars)
 - #152591 (Simplify internals of `{Rc,Arc}::default`)
 - #152865 (Fixed ByteStr not padding within its Display trait when no specific alignment is mentioned)
 - #152908 (Enable rust.remap-debuginfo in the dist profile)
 - #147859 (reduce the amount of panics in `{TokenStream, Literal}::from_str` calls)
 - #152705 (Test(lib/win/proc): Skip `raw_attributes` doctest under Win7)
 - #152767 (fix typo in `carryless_mul` macro invocation)
 - #152837 (fix(codegen): Use `body_codegen_attrs` For Caller In `adjust_target_feature_sig`)
 - #152871 (Fix warnings in rs{begin,end}.rs files)
 - #152879 (Remove `impl IntoQueryParam<P> for &'a P`.)
 - #152933 (Start migration for `LintDiagnostic` items by adding API and migrating `LinkerOutput` lint)
 - #152937 (remove unneeded reboxing)
 - #152953 (Fix typo in armv7a-vex-v5.md)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 22, 2026
…rs-const-caller, r=dingxiangfei2009

fix(codegen): Use `body_codegen_attrs` For Caller In `adjust_target_feature_sig`

### Summary:

#### Problem:

Coercing a `#[target_feature]` `const fn` to a function pointer inside a `const` body triggers an ICE (debug builds only):

```rust
#[target_feature(enable = "sse2")]
const fn with_target_feature() {}

const X: () = unsafe {
    let _: unsafe fn() = with_target_feature; // ICE
};
```

```text
assertion failed: def_kind.has_codegen_attrs()
unexpected `def_kind` in `codegen_fn_attrs`: Const
```

#### Root Cause:

Introduced in rust-lang#135504 (2025-01-14, commit `8fee6a77394`). `adjust_target_feature_sig` unconditionally calls `codegen_fn_attrs(caller)` to get the caller's target features. `codegen_fn_attrs` requires that the `DefId` satisfies `has_codegen_attrs()`. `DefKind::Const`, `AssocConst`, and `InlineConst` do not — they have no codegen attributes by design. The debug assertion fires.

In release builds the call "worked" accidentally: `codegen_fn_attrs` on a const would reach the query machinery and happen to return empty attributes, producing a correct (but unguaranteed) result. The bug was latent until debug builds exposed it.

#### Solution:

Replace `codegen_fn_attrs(caller)` with `body_codegen_attrs(caller)`. `body_codegen_attrs` exists precisely for this case: it delegates to `codegen_fn_attrs` for function-like `DefKind`s and returns `CodegenFnAttrs::EMPTY` for const items. A const body has no target features, so returning empty is semantically correct.

Also fix the pre-existing variable name `callee_features` → `caller_features` (the variable holds the *caller*'s features, not the callee's).

### Changes:

- `compiler/rustc_middle/src/ty/context.rs`: `adjust_target_feature_sig` — use `body_codegen_attrs` for `caller`; rename `callee_features` → `caller_features`.
- `tests/ui/target-feature/const-target-feature-fn-ptr-coercion.rs`: regression test covering `Const`, `AssocConst`, and `InlineConst` caller contexts.

Fixes rust-lang#152340.
rust-bors bot pushed a commit that referenced this pull request Feb 22, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - #150468 (rustc_target: callconv: powerpc64: Use the ABI set in target options instead of guessing)
 - #151628 (Fix ICE in const eval of packed SIMD types with non-power-of-two element counts)
 - #151871 (don't use env with infer vars)
 - #152591 (Simplify internals of `{Rc,Arc}::default`)
 - #152865 (Fixed ByteStr not padding within its Display trait when no specific alignment is mentioned)
 - #147859 (reduce the amount of panics in `{TokenStream, Literal}::from_str` calls)
 - #152705 (Test(lib/win/proc): Skip `raw_attributes` doctest under Win7)
 - #152767 (fix typo in `carryless_mul` macro invocation)
 - #152837 (fix(codegen): Use `body_codegen_attrs` For Caller In `adjust_target_feature_sig`)
 - #152871 (Fix warnings in rs{begin,end}.rs files)
 - #152879 (Remove `impl IntoQueryParam<P> for &'a P`.)
 - #152933 (Start migration for `LintDiagnostic` items by adding API and migrating `LinkerOutput` lint)
 - #152937 (remove unneeded reboxing)
 - #152953 (Fix typo in armv7a-vex-v5.md)
rust-bors bot pushed a commit that referenced this pull request Feb 22, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - #150468 (rustc_target: callconv: powerpc64: Use the ABI set in target options instead of guessing)
 - #151628 (Fix ICE in const eval of packed SIMD types with non-power-of-two element counts)
 - #151871 (don't use env with infer vars)
 - #152591 (Simplify internals of `{Rc,Arc}::default`)
 - #152865 (Fixed ByteStr not padding within its Display trait when no specific alignment is mentioned)
 - #147859 (reduce the amount of panics in `{TokenStream, Literal}::from_str` calls)
 - #152705 (Test(lib/win/proc): Skip `raw_attributes` doctest under Win7)
 - #152767 (fix typo in `carryless_mul` macro invocation)
 - #152837 (fix(codegen): Use `body_codegen_attrs` For Caller In `adjust_target_feature_sig`)
 - #152871 (Fix warnings in rs{begin,end}.rs files)
 - #152879 (Remove `impl IntoQueryParam<P> for &'a P`.)
 - #152933 (Start migration for `LintDiagnostic` items by adding API and migrating `LinkerOutput` lint)
 - #152937 (remove unneeded reboxing)
 - #152953 (Fix typo in armv7a-vex-v5.md)
@rust-bors rust-bors bot merged commit 4486e4f into rust-lang:main Feb 22, 2026
11 checks passed
@rustbot rustbot added this to the 1.95.0 milestone Feb 22, 2026
rust-timer added a commit that referenced this pull request Feb 22, 2026
Rollup merge of #152837 - TKanX:bugfix/152340-codegen-fn-attrs-const-caller, r=dingxiangfei2009

fix(codegen): Use `body_codegen_attrs` For Caller In `adjust_target_feature_sig`

### Summary:

#### Problem:

Coercing a `#[target_feature]` `const fn` to a function pointer inside a `const` body triggers an ICE (debug builds only):

```rust
#[target_feature(enable = "sse2")]
const fn with_target_feature() {}

const X: () = unsafe {
    let _: unsafe fn() = with_target_feature; // ICE
};
```

```text
assertion failed: def_kind.has_codegen_attrs()
unexpected `def_kind` in `codegen_fn_attrs`: Const
```

#### Root Cause:

Introduced in #135504 (2025-01-14, commit `8fee6a77394`). `adjust_target_feature_sig` unconditionally calls `codegen_fn_attrs(caller)` to get the caller's target features. `codegen_fn_attrs` requires that the `DefId` satisfies `has_codegen_attrs()`. `DefKind::Const`, `AssocConst`, and `InlineConst` do not — they have no codegen attributes by design. The debug assertion fires.

In release builds the call "worked" accidentally: `codegen_fn_attrs` on a const would reach the query machinery and happen to return empty attributes, producing a correct (but unguaranteed) result. The bug was latent until debug builds exposed it.

#### Solution:

Replace `codegen_fn_attrs(caller)` with `body_codegen_attrs(caller)`. `body_codegen_attrs` exists precisely for this case: it delegates to `codegen_fn_attrs` for function-like `DefKind`s and returns `CodegenFnAttrs::EMPTY` for const items. A const body has no target features, so returning empty is semantically correct.

Also fix the pre-existing variable name `callee_features` → `caller_features` (the variable holds the *caller*'s features, not the callee's).

### Changes:

- `compiler/rustc_middle/src/ty/context.rs`: `adjust_target_feature_sig` — use `body_codegen_attrs` for `caller`; rename `callee_features` → `caller_features`.
- `tests/ui/target-feature/const-target-feature-fn-ptr-coercion.rs`: regression test covering `Const`, `AssocConst`, and `InlineConst` caller contexts.

Fixes #152340.
@TKanX TKanX deleted the bugfix/152340-codegen-fn-attrs-const-caller branch February 22, 2026 23:08
github-actions bot pushed a commit to rust-lang/rust-analyzer that referenced this pull request Feb 23, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - rust-lang/rust#150468 (rustc_target: callconv: powerpc64: Use the ABI set in target options instead of guessing)
 - rust-lang/rust#151628 (Fix ICE in const eval of packed SIMD types with non-power-of-two element counts)
 - rust-lang/rust#151871 (don't use env with infer vars)
 - rust-lang/rust#152591 (Simplify internals of `{Rc,Arc}::default`)
 - rust-lang/rust#152865 (Fixed ByteStr not padding within its Display trait when no specific alignment is mentioned)
 - rust-lang/rust#147859 (reduce the amount of panics in `{TokenStream, Literal}::from_str` calls)
 - rust-lang/rust#152705 (Test(lib/win/proc): Skip `raw_attributes` doctest under Win7)
 - rust-lang/rust#152767 (fix typo in `carryless_mul` macro invocation)
 - rust-lang/rust#152837 (fix(codegen): Use `body_codegen_attrs` For Caller In `adjust_target_feature_sig`)
 - rust-lang/rust#152871 (Fix warnings in rs{begin,end}.rs files)
 - rust-lang/rust#152879 (Remove `impl IntoQueryParam<P> for &'a P`.)
 - rust-lang/rust#152933 (Start migration for `LintDiagnostic` items by adding API and migrating `LinkerOutput` lint)
 - rust-lang/rust#152937 (remove unneeded reboxing)
 - rust-lang/rust#152953 (Fix typo in armv7a-vex-v5.md)
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Feb 23, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - rust-lang/rust#150468 (rustc_target: callconv: powerpc64: Use the ABI set in target options instead of guessing)
 - rust-lang/rust#151628 (Fix ICE in const eval of packed SIMD types with non-power-of-two element counts)
 - rust-lang/rust#151871 (don't use env with infer vars)
 - rust-lang/rust#152591 (Simplify internals of `{Rc,Arc}::default`)
 - rust-lang/rust#152865 (Fixed ByteStr not padding within its Display trait when no specific alignment is mentioned)
 - rust-lang/rust#147859 (reduce the amount of panics in `{TokenStream, Literal}::from_str` calls)
 - rust-lang/rust#152705 (Test(lib/win/proc): Skip `raw_attributes` doctest under Win7)
 - rust-lang/rust#152767 (fix typo in `carryless_mul` macro invocation)
 - rust-lang/rust#152837 (fix(codegen): Use `body_codegen_attrs` For Caller In `adjust_target_feature_sig`)
 - rust-lang/rust#152871 (Fix warnings in rs{begin,end}.rs files)
 - rust-lang/rust#152879 (Remove `impl IntoQueryParam<P> for &'a P`.)
 - rust-lang/rust#152933 (Start migration for `LintDiagnostic` items by adding API and migrating `LinkerOutput` lint)
 - rust-lang/rust#152937 (remove unneeded reboxing)
 - rust-lang/rust#152953 (Fix typo in armv7a-vex-v5.md)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-codegen Area: Code generation A-target-feature Area: Enabling/disabling target features like AVX, Neon, etc. C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

[ICE]: unexpected def_kind in codegen_fn_attrs: Const

5 participants