dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.91k stars 4.63k forks source link

[MSVC][permissive] Resolving C4146 Error in MSVC Compilation with /permissive Flag #102708

Open NEIL-smtg opened 3 months ago

NEIL-smtg commented 3 months ago

Description

Since commit 0ce3c32, compilation with the /permissive flag in Microsoft Visual C++ (MSVC), the FloatingPointUtils::ilogb function encounters a C4146 error. This error arises due to the unary minus operator being applied to an unsigned type, particularly when dealing with the value -2147483648. I have personally tested the code with INT_MIN and INT_MAX constants, and the code compiles without any errors.

Log: build.log

Reproduction Steps

  1. Open x64 Native Tools Command prompt for VS 2022
  2. git clone https://github.com/dotnet/runtime.git
  3. cd runtime
  4. cl.exe /nologo /TP -DDEBUGGING_SUPPORTED -DFEATURE_ARRAYSTUB_AS_IL -DFEATURE_BASICFREEZE -DFEATURE_CODE_VERSIONING -DFEATURE_COLLECTIBLE_TYPES -DFEATURE_COMINTEROP -DFEATURE_COMINTEROP_APARTMENT_SUPPORT -DFEATURE_COMINTEROP_UNMANAGED_ACTIVATION -DFEATURE_COMWRAPPERS -DFEATURE_CORECLR -DFEATURE_DATABREAKPOINT -DFEATURE_DEFAULT_INTERFACES -DFEATURE_EVENT_TRACE -DFEATURE_HIJACK -DFEATURE_HW_INTRINSICS -DFEATURE_ICASTABLE -DFEATURE_IJW -DFEATURE_INSTANTIATINGSTUB_AS_IL -DFEATURE_INTEROP_DEBUGGING -DFEATURE_ISYM_READER -DFEATURE_MANUALLY_MANAGED_CARD_BUNDLES -DFEATURE_MASKED_HW_INTRINSICS -DFEATURE_METADATA_UPDATER -DFEATURE_MULTICASTSTUB_AS_IL -DFEATURE_MULTICOREJIT -DFEATURE_NO_HOST -DFEATURE_ON_STACK_REPLACEMENT -DFEATURE_PERFTRACING -DFEATURE_PGO -DFEATURE_PORTABLE_SHUFFLE_THUNKS -DFEATURE_PROFAPI_ATTACH_DETACH -DFEATURE_READYTORUN -DFEATURE_REJIT -DFEATURE_REMAP_FUNCTION -DFEATURE_SIMD -DFEATURE_SPECIAL_USER_MODE_APC -DFEATURE_STANDALONE_GC -DFEATURE_SVR_GC -DFEATURE_SYMDIFF -DFEATURE_TIERED_COMPILATION -DFEATURE_TYPEEQUIVALENCE -DFEATURE_USE_ASM_GC_WRITE_BARRIERS -DFEATURE_USE_SOFTWARE_WRITE_WATCH_FOR_GC_HEAP -DFX_VER_INTERNALNAME_STR=clrjit.dll -DHOST_64BIT -DHOST_AMD64 -DHOST_WINDOWS -DJIT_BUILD -DNDEBUG -DNOMINMAX -DOUT_OF_PROCESS_SETTHREADCONTEXT -DPROFILING_SUPPORTED -DSELF_NO_HOST -DTARGET_64BIT -DTARGET_AMD64 -DTARGET_WINDOWS -DUNICODE -DUNIX_AMD64_ABI_ITF -DURTBLDENV_FRIENDLY=Retail -DWIN32 -DWIN32_LEAN_AND_MEAN -DWINVER=0x0602 -DWITH_NATIVE_PGO -D_BLD_CLR -D_CRT_SECURE_NO_WARNINGS -D_FILE_OFFSET_BITS=64 -D_SECURE_SCL=0 -D_TIME_BITS=64 -D_UNICODE -D_WIN32 -D_WIN32_WINNT=0x0602 -Dclrjit_EXPORTS -IC:\gitP\dotnet\runtime\artifacts\obj\coreclr\windows.x64.Release\jit -IC:\gitP\dotnet\runtime\src\coreclr\jit -IC:\gitP\dotnet\runtime\src\native -IC:\gitP\dotnet\runtime\src\native\inc -IC:\gitP\dotnet\runtime\src\coreclr\pal\prebuilt\inc -IC:\gitP\dotnet\runtime\artifacts\obj -IC:\gitP\dotnet\runtime\src\coreclr\inc -IC:\gitP\dotnet\runtime\src\coreclr\debug\inc -IC:\gitP\dotnet\runtime\src\coreclr\debug\inc\amd64 -IC:\gitP\dotnet\runtime\src\coreclr\debug\inc\dump -IC:\gitP\dotnet\runtime\src\coreclr\md\inc -IC:\gitP\dotnet\runtime\src\coreclr\classlibnative\bcltype -IC:\gitP\dotnet\runtime\src\coreclr\classlibnative\inc -IC:\gitP\dotnet\runtime\artifacts\obj\coreclr\windows.x64.Release\inc -IC:\gitP\dotnet\runtime\src\coreclr\hosts\inc -IC:\gitP\dotnet\runtime\src\coreclr\minipal -IC:\gitP\dotnet\runtime\artifacts\obj\coreclr\windows.x64.Release\inc\etw -IC:\gitP\dotnet\runtime\src\coreclr\jit.\jitstd -IC:\gitP\dotnet\runtime\src\coreclr\jit..\inc /DWIN32 /D_WINDOWS /GR- /O2 /Ob2 /DNDEBUG -MT /GL /Ox /EHa /nologo /W4 /WX /Oi /Oy- /Gm- /Zp8 /Gy /GS /fp:precise /FC /MP /Zm200 /Zc:strictStrings /Zc:wchar_t /Zc:inline /Zc:forScope /wd4065 /wd4100 /wd4127 /wd4131 /wd4189 /wd4200 /wd4201 /wd4206 /wd4239 /wd4245 /wd4291 /wd4310 /wd4324 /wd4366 /wd4456 /wd4457 /wd4458 /wd4459 /wd4463 /wd4505 /wd4702 /wd4706 /wd4733 /wd4815 /wd4838 /wd4918 /wd4960 /wd4961 /wd5105 /wd5205 /we4007 /we4013 /we4102 /we4551 /we4640 /we4806 /we4055 /we4146 /we4242 /we4244 /we4267 /we4302 /we4308 /we4509 /we4510 /we4532 /we4533 /we4610 /we4611 /we4700 /we4701 /we4703 /we4789 /we4995 /we4996 /w34092 /w34121 /w34125 /w34130 /w34132 /w34212 /w34530 /w35038 /w44177 /Zi /ZH:SHA_256 /source-charset:utf-8 /guard:cf /guard:ehcont /Zl /permissive -c C:\gitP\dotnet\runtime\src\coreclr\jit\utils.cpp

Expected behavior

It compiles.

Actual behavior

error C4146: unary minus operator applied to unsigned type, result still unsigned
error C4146: unary minus operator applied to unsigned type, result still unsigned

Regression?

0ce3c32

Known Workarounds

int FloatingPointUtils::ilogb(double value)
{
    if (value == 0.0)
    {
        return INT_MIN;
    }
    else if (isNaN(value))
    {
        return INT_MAX;
    }
    return ilogb(value);
}

int FloatingPointUtils::ilogb(float value)
{
    if (value == 0.0f)
    {
        return INT_MIN;
    }
    else if (isNaN(value))
    {
        return INT_MAX;
    }
    return ilogbf(value);
}

Configuration

Architecture : x64 OS : Windows Server 2022 Datacenter

Other information

No response

dotnet-policy-service[bot] commented 3 months ago

Tagging subscribers to this area: @hoyosjs See info in area-owners.md if you want to be subscribed.