Skip to content

Annotate System.IO.Ports for nullable reference types - #136828

Merged
jkotas merged 13 commits into
dotnet:mainfrom
sumit-2007-git:annotate-system-io-ports
Nov 3, 2026
Merged

jkotas merged 13 commits into
dotnet:mainfrom
sumit-2007-git:annotate-system-io-ports

Conversation

@sumit-2007-git

Copy link
Copy Markdown
Contributor

Contributes to #90400

This PR enables nullable reference types and annotates System.IO.Ports:

  • Removed <Nullable>disable</Nullable> and <NoWarn>$(NoWarn);nullable</NoWarn> from ref/System.IO.Ports.csproj and src/System.IO.Ports.csproj.
  • Annotated ref/System.IO.Ports.cs events as nullable delegates (DataReceived, ErrorReceived, PinChanged).
  • Annotated SerialPort, SerialStream, and platform-specific implementations (Windows and Unix) with appropriate nullable reference types and attributes ([MemberNotNullWhen], [MemberNotNull]).

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 2, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-io-ports
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The nullable contracts misrepresent runtime behavior: GetPortNames() can return null elements after a registry value disappears, while WriteLine’s non-null parameter annotation warns on valid null arguments.

2 open issues
What changed in this PR

This PR enables nullable reference types in System.IO.Ports, contributing to the remaining-library annotation work tracked by #90400.

Changes:

  • Removes nullable opt-outs from the implementation and reference projects.
  • Annotates public events and internal state across Windows and Unix implementations.
  • Adds flow-analysis attributes to handle checks and throwing helpers.
File Description
src/​libraries/​System.IO.Ports/​src/​System/​IO/​Ports/​SerialStream.Windows.cs Annotates handles, events, callbacks, and asynchronous state; adds null-safe disposal.
src/​libraries/​System.IO.Ports/​src/​System/​IO/​Ports/​SerialStream.Unix.cs Annotates handles, events, queued requests, and timeout helpers.
src/​libraries/​System.IO.Ports/​src/​System/​IO/​Ports/​SerialStream.cs Adds nullable event and handle-validation attributes.
src/​libraries/​System.IO.Ports/​src/​System/​IO/​Ports/​SerialPort.Windows.cs Annotates registry access used for port enumeration.
src/​libraries/​System.IO.Ports/​src/​System/​IO/​Ports/​SerialPort.cs Annotates events, internal state, and the open-state contract.
src/​libraries/​System.IO.Ports/​src/​System/​IO/​Ports/​InternalResources.cs Marks throwing helpers with DoesNotReturn.
src/​libraries/​System.IO.Ports/​src/​System.IO.Ports.csproj Enables nullable analysis for the implementation.
src/​libraries/​System.IO.Ports/​ref/​System.IO.Ports.csproj Enables nullable annotations for the reference assembly.
src/​libraries/​System.IO.Ports/​ref/​System.IO.Ports.cs Publishes nullable event delegate annotations.

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The change introduces an API behavior regression by making Write(null) silently succeed on an open port instead of throwing ArgumentNullException.

2 open issues
1 resolved since last review

Review effort: Balanced

@jkotas

jkotas commented Nov 3, 2026

Copy link
Copy Markdown
Member

/ba-g opened #136892 on the test failure

@jkotas
jkotas merged commit 3f76661 into dotnet:main Nov 3, 2026
85 of 87 checks passed
@sumit-2007-git

Copy link
Copy Markdown
Contributor Author

Thank you so much @jkotas for reviewing and merging this PR! Really appreciate your time, patient guidance, and support throughout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.IO.Ports community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants